Skip to content

Commit 7be4936

Browse files
authored
Merge pull request #240 from serilog/dev
Release 5.2.0
2 parents 9a978c4 + 01c2310 commit 7be4936

38 files changed

+902
-215
lines changed

CODE_OF_CONDUCT.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Code of Conduct
2+
3+
Please refer to the [Serilog Code of Conduct](https://github.com/serilog/serilog/blob/dev/CODE_OF_CONDUCT.md) which covers all repositories within the Serilog Organization.

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ All sink configuration methods accept the following arguments, though not necess
3535
* `batchPostingLimit`
3636
* `period`
3737
* `formatProvider`
38+
* `logEventFormatter`
3839

3940
### Basic Arguments
4041

@@ -54,6 +55,8 @@ Consider increasing the batch size in high-volume logging environments. In one t
5455

5556
Refer to the Serilog Wiki's explanation of [Format Providers](https://github.com/serilog/serilog/wiki/Formatting-Output#format-providers) for details about the `formatProvider` arguments.
5657

58+
The parameter `logEventFormatter` can be used to specify a custom renderer implementing `ITextFormatter` which will be used to generate the contents of the `LogEvent`column. If the parameter is omitted or set to null, the default internal JSON formatter will be used. For more information about custom text formatters refer to the Serilog documentation [Custom text formatters](https://github.com/serilog/serilog/wiki/Formatting-Output#custom-text-formatters).
59+
5760
### Platform-Specific Arguments
5861

5962
These additional arguments are accepted when the sink is configured from a library or application that supports the .NET Standard-style _Microsoft.Extensions.Configuration_ packages. They are optional.
@@ -154,6 +157,7 @@ The constructor accepts most of the same arguments, and like other Serilog audit
154157
* `autoCreateSqlTable`
155158
* `columnOptions`
156159
* `formatProvider`
160+
* `logEventFormatter`
157161

158162
The `restrictedToMinimumLevel` parameter is not available because all events written to an audit sink are required to succeed.
159163

@@ -408,6 +412,8 @@ This column stores log event property values as JSON. Typically you will use eit
408412

409413
The `ExcludeAddtionalProperties` and `ExcludeStandardColumns` properties are described in the [Custom Property Columns](#custom-property-columns) topic.
410414

415+
The content of this column is rendered as JSON by default or with a custom ITextFormatter passed by the caller as parameter `logEventFormatter`. Details can be found in [Sink Configuration Options](#sink-configuration-options).
416+
411417
## Custom Property Columns
412418

413419
By default, any log event properties you include in your log statements will be saved to the XML `Properties` column or the JSON `LogEvent` column. But they can also be stored in their own individual columns via the `AdditionalColumns` collection. This adds overhead to write operations but is very useful for frequently-queried properties. Only `ColumnName` is required; the default configuration is `varchar(max)`.
@@ -554,12 +560,12 @@ Keys and values are case-sensitive. Case must match **_exactly_** as shown below
554560
<remove Name="Properties"/>
555561
</RemoveStandardColumns>
556562
<Columns>
557-
<add columnName="EventType" DataType="int"/>
558-
<add columnName="Release"
559-
dataType="varchar"
560-
dataLength="64"
561-
allowNull="true"
562-
nonClusteredIndex="false"/>
563+
<add ColumnName="EventType" DataType="int"/>
564+
<add ColumnName="Release"
565+
DataType="varchar"
566+
DataLength="64"
567+
AllowNull="true"
568+
NonClusteredIndex="false"/>
563569
</Columns>
564570
<Exception ColumnName="Ex" DataLength="512"/>
565571
<Id NonClusteredIndex="true"/>

assets/CommonAssemblyInfo.cs

Lines changed: 0 additions & 5 deletions
This file was deleted.

sample/AppConfigDemo/App.config

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<configSections>
4+
<section name="MSSqlServerSettingsSection" type="Serilog.Configuration.MSSqlServerConfigurationSection, Serilog.Sinks.MSSqlServer" />
5+
</configSections>
6+
<startup>
7+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
8+
</startup>
9+
<MSSqlServerSettingsSection>
10+
<AddStandardColumns>
11+
<add Name="LogEvent" />
12+
</AddStandardColumns>
13+
<RemoveStandardColumns>
14+
<remove Name="Properties" />
15+
</RemoveStandardColumns>
16+
<TimeStamp ColumnName="TimeStampAlternative" ConvertToUtc="true" />
17+
</MSSqlServerSettingsSection>
18+
<runtime>
19+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
20+
<dependentAssembly>
21+
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
22+
<bindingRedirect oldVersion="0.0.0.0-4.0.6.0" newVersion="4.0.6.0" />
23+
</dependentAssembly>
24+
<dependentAssembly>
25+
<assemblyIdentity name="Microsoft.Extensions.Primitives" publicKeyToken="adb9793829ddae60" culture="neutral" />
26+
<bindingRedirect oldVersion="0.0.0.0-3.1.2.0" newVersion="3.1.2.0" />
27+
</dependentAssembly>
28+
</assemblyBinding>
29+
</runtime>
30+
</configuration>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{7DC530B1-68FD-4F07-A2F9-910C338562C1}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>AppConfigDemo</RootNamespace>
10+
<AssemblyName>AppConfigDemo</AssemblyName>
11+
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="Microsoft.Extensions.Configuration.Abstractions, Version=3.1.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
37+
<HintPath>..\..\packages\Microsoft.Extensions.Configuration.Abstractions.3.1.2\lib\netstandard2.0\Microsoft.Extensions.Configuration.Abstractions.dll</HintPath>
38+
</Reference>
39+
<Reference Include="Microsoft.Extensions.Primitives, Version=3.1.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
40+
<HintPath>..\..\packages\Microsoft.Extensions.Primitives.3.1.2\lib\netstandard2.0\Microsoft.Extensions.Primitives.dll</HintPath>
41+
</Reference>
42+
<Reference Include="Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10, processorArchitecture=MSIL">
43+
<HintPath>..\..\packages\Serilog.2.6.0\lib\net46\Serilog.dll</HintPath>
44+
</Reference>
45+
<Reference Include="System" />
46+
<Reference Include="System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
47+
<HintPath>..\..\packages\System.Buffers.4.4.0\lib\netstandard2.0\System.Buffers.dll</HintPath>
48+
</Reference>
49+
<Reference Include="System.Configuration" />
50+
<Reference Include="System.Core" />
51+
<Reference Include="System.Memory, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
52+
<HintPath>..\..\packages\System.Memory.4.5.2\lib\netstandard2.0\System.Memory.dll</HintPath>
53+
</Reference>
54+
<Reference Include="System.Numerics" />
55+
<Reference Include="System.Numerics.Vectors, Version=4.1.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
56+
<HintPath>..\..\packages\System.Numerics.Vectors.4.4.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
57+
</Reference>
58+
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.6.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
59+
<HintPath>..\..\packages\System.Runtime.CompilerServices.Unsafe.4.7.0\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
60+
</Reference>
61+
<Reference Include="System.Xml.Linq" />
62+
<Reference Include="System.Data.DataSetExtensions" />
63+
<Reference Include="Microsoft.CSharp" />
64+
<Reference Include="System.Data" />
65+
<Reference Include="System.Net.Http" />
66+
<Reference Include="System.Xml" />
67+
</ItemGroup>
68+
<ItemGroup>
69+
<Compile Include="Program.cs" />
70+
<Compile Include="Properties\AssemblyInfo.cs" />
71+
</ItemGroup>
72+
<ItemGroup>
73+
<None Include="App.config" />
74+
<None Include="packages.config" />
75+
</ItemGroup>
76+
<ItemGroup>
77+
<ProjectReference Include="..\..\src\Serilog.Sinks.MSSqlServer\Serilog.Sinks.MSSqlServer.csproj">
78+
<Project>{803cd13a-d54b-4cec-a55f-e22ae3d93b3c}</Project>
79+
<Name>Serilog.Sinks.MSSqlServer</Name>
80+
</ProjectReference>
81+
</ItemGroup>
82+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
83+
</Project>

sample/AppConfigDemo/Program.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using Serilog;
2+
using Serilog.Events;
3+
using Serilog.Sinks.MSSqlServer;
4+
using System;
5+
using System.Threading;
6+
7+
namespace AppConfigDemo
8+
{
9+
public class Program
10+
{
11+
const string _connectionString = "Server=localhost;Database=LogTest;Integrated Security=SSPI;";
12+
const string _schemaName = "dbo";
13+
const string _tableName = "LogEvents";
14+
15+
public static void Main()
16+
{
17+
Log.Logger = new LoggerConfiguration().WriteTo
18+
.MSSqlServer(
19+
connectionString: _connectionString,
20+
tableName: _tableName,
21+
restrictedToMinimumLevel: LogEventLevel.Debug,
22+
batchPostingLimit: MSSqlServerSink.DefaultBatchPostingLimit,
23+
period: null,
24+
formatProvider: null,
25+
autoCreateSqlTable: true,
26+
columnOptions: null,
27+
schemaName: _schemaName,
28+
logEventFormatter: null)
29+
.CreateLogger();
30+
31+
Log.Debug("Getting started");
32+
33+
Log.Information("Hello {Name} from thread {ThreadId}", Environment.GetEnvironmentVariable("USERNAME"),
34+
Thread.CurrentThread.ManagedThreadId);
35+
36+
Log.Warning("No coins remain at position {@Position}", new { Lat = 25, Long = 134 });
37+
38+
Log.CloseAndFlush();
39+
}
40+
}
41+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("AppConfigDemo")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("AppConfigDemo")]
13+
[assembly: AssemblyCopyright("Copyright © 2020")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("7dc530b1-68fd-4f07-a2f9-910c338562c1")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

sample/AppConfigDemo/packages.config

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Microsoft.Extensions.Configuration.Abstractions" version="3.1.2" targetFramework="net461" />
4+
<package id="Microsoft.Extensions.Primitives" version="3.1.2" targetFramework="net461" />
5+
<package id="Serilog" version="2.6.0" targetFramework="net461" />
6+
<package id="Serilog.Settings.AppSettings" version="2.2.2" targetFramework="net461" />
7+
<package id="System.Buffers" version="4.4.0" targetFramework="net461" />
8+
<package id="System.Memory" version="4.5.2" targetFramework="net461" />
9+
<package id="System.Numerics.Vectors" version="4.4.0" targetFramework="net461" />
10+
<package id="System.Runtime.CompilerServices.Unsafe" version="4.7.0" targetFramework="net461" />
11+
</packages>
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+
public 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+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using Serilog.Events;
2+
using Serilog.Formatting;
3+
using System.IO;
4+
using System.Linq;
5+
6+
namespace WorkerServiceDemo
7+
{
8+
public class CustomLogEventFormatter : ITextFormatter
9+
{
10+
public static CustomLogEventFormatter Formatter { get; } = new CustomLogEventFormatter();
11+
12+
public void Format(LogEvent logEvent, TextWriter output)
13+
{
14+
logEvent.Properties.ToList()
15+
.ForEach(e => output.Write($"{e.Key}={e.Value} "));
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)