Skip to content

Commit 76d44e6

Browse files
author
Kadluba Christian
committed
Added a small sample app that demonstrates using MSSqlServerSink with a custom LogEvent column formatter.
1 parent f905e94 commit 76d44e6

File tree

4 files changed

+99
-2
lines changed

4 files changed

+99
-2
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<ProjectReference Include="..\..\src\Serilog.Sinks.MSSqlServer\Serilog.Sinks.MSSqlServer.csproj" />
10+
</ItemGroup>
11+
12+
</Project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using Serilog.Events;
2+
using Serilog.Formatting;
3+
using System.IO;
4+
using System.Linq;
5+
6+
namespace CustomLogEventFormatterDemo
7+
{
8+
public class FlatLogEventFormatter : ITextFormatter
9+
{
10+
public void Format(LogEvent logEvent, TextWriter output)
11+
{
12+
logEvent.Properties.ToList().ForEach(e =>
13+
{
14+
output.Write($"{e.Key}={e.Value} ");
15+
});
16+
output.WriteLine();
17+
}
18+
}
19+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using Serilog;
2+
using Serilog.Sinks.MSSqlServer;
3+
using System;
4+
using System.Threading;
5+
6+
namespace CustomLogEventFormatterDemo
7+
{
8+
public class Program
9+
{
10+
const string _connectionString = "Server=localhost;Database=LogTest;Integrated Security=SSPI;";
11+
const string _schemaName = "dbo";
12+
const string _tableName = "LogEvents";
13+
14+
static void Main()
15+
{
16+
var options = new ColumnOptions();
17+
options.Store.Add(StandardColumn.LogEvent);
18+
var customFormatter = new FlatLogEventFormatter();
19+
Log.Logger = new LoggerConfiguration()
20+
.WriteTo.MSSqlServer(_connectionString,
21+
_tableName,
22+
appConfiguration: null,
23+
restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Verbose,
24+
batchPostingLimit: 50,
25+
period: null,
26+
formatProvider: null,
27+
autoCreateSqlTable: true,
28+
columnOptions: options,
29+
columnOptionsSection: null,
30+
schemaName: _schemaName,
31+
logEventFormatter: customFormatter)
32+
.CreateLogger();
33+
34+
try
35+
{
36+
Log.Debug("Getting started");
37+
38+
Log.Information("Hello {Name} from thread {ThreadId}", Environment.GetEnvironmentVariable("USERNAME"), Thread.CurrentThread.ManagedThreadId);
39+
40+
Log.Warning("No coins remain at position {@Position}", new { Lat = 25, Long = 134 });
41+
42+
Fail();
43+
}
44+
catch (DivideByZeroException e)
45+
{
46+
Log.Error(e, "Division by zero");
47+
}
48+
49+
Log.CloseAndFlush();
50+
}
51+
52+
static void Fail()
53+
{
54+
throw new DivideByZeroException();
55+
}
56+
}
57+
}

serilog-sinks-mssqlserver.sln

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26730.16
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.29806.167
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{04226074-C72F-42BC-AB02-4D70A7BAE7E1}"
77
EndProject
@@ -19,6 +19,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Sinks.MSSqlServer",
1919
EndProject
2020
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Serilog.Sinks.MSSqlServer.Tests", "test\Serilog.Sinks.MSSqlServer.Tests\Serilog.Sinks.MSSqlServer.Tests.csproj", "{3C2D8E01-5580-426A-BDD9-EC59CD98E618}"
2121
EndProject
22+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sample", "sample", "{AA346332-5BAF-47F1-B8FB-7600ED61265D}"
23+
EndProject
24+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CustomLogEventFormatterDemo", "sample\CustomLogEventFormatterDemo\CustomLogEventFormatterDemo.csproj", "{873320F1-8F6D-45E2-A853-D321C86793EC}"
25+
EndProject
2226
Global
2327
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2428
Debug|Any CPU = Debug|Any CPU
@@ -33,13 +37,18 @@ Global
3337
{3C2D8E01-5580-426A-BDD9-EC59CD98E618}.Debug|Any CPU.Build.0 = Debug|Any CPU
3438
{3C2D8E01-5580-426A-BDD9-EC59CD98E618}.Release|Any CPU.ActiveCfg = Release|Any CPU
3539
{3C2D8E01-5580-426A-BDD9-EC59CD98E618}.Release|Any CPU.Build.0 = Release|Any CPU
40+
{873320F1-8F6D-45E2-A853-D321C86793EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
41+
{873320F1-8F6D-45E2-A853-D321C86793EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
42+
{873320F1-8F6D-45E2-A853-D321C86793EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
43+
{873320F1-8F6D-45E2-A853-D321C86793EC}.Release|Any CPU.Build.0 = Release|Any CPU
3644
EndGlobalSection
3745
GlobalSection(SolutionProperties) = preSolution
3846
HideSolutionNode = FALSE
3947
EndGlobalSection
4048
GlobalSection(NestedProjects) = preSolution
4149
{803CD13A-D54B-4CEC-A55F-E22AE3D93B3C} = {04226074-C72F-42BC-AB02-4D70A7BAE7E1}
4250
{3C2D8E01-5580-426A-BDD9-EC59CD98E618} = {F02D6513-6F45-452E-85A0-41A872A2C1F8}
51+
{873320F1-8F6D-45E2-A853-D321C86793EC} = {AA346332-5BAF-47F1-B8FB-7600ED61265D}
4352
EndGlobalSection
4453
GlobalSection(ExtensibilityGlobals) = postSolution
4554
SolutionGuid = {AAA6BF8D-7B53-4A5F-A79A-D1B306383B45}

0 commit comments

Comments
 (0)