-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from osstotalsoft/feature/serilog-extensions
serilog extensions
- Loading branch information
Showing
3 changed files
with
190 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
src/Correlation/NBB.Correlation.Serilog.SqlServer/LoggerConfigurationExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
using Serilog; | ||
using Serilog.Configuration; | ||
using Serilog.Events; | ||
using System; | ||
using System.Collections.Generic; | ||
using Serilog.Sinks.MSSqlServer; | ||
using System.Linq; | ||
|
||
namespace NBB.Correlation.Serilog.SqlServer | ||
{ | ||
public static class LoggerConfigurationExtensions | ||
{ | ||
/// <summary> | ||
/// Adds a sink that writes log events to a table in a MSSqlServer database. | ||
/// Adds a column named CorrelationId to the resulting table | ||
/// Create a database and execute the table creation script found here | ||
/// https://gist.github.com/mivano/10429656 | ||
/// or use the autoCreateSqlTable option. | ||
/// </summary> | ||
/// <param name="loggerConfiguration">The logger configuration.</param> | ||
/// <param name="connectionString">The connection string to the database where to store the events.</param> | ||
/// <param name="tableName">Name of the table to store the events in.</param> | ||
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param> | ||
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param> | ||
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param> | ||
/// <param name="period">The time to wait between checking for event batches.</param> | ||
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param> | ||
/// <param name="autoCreateSqlTable">Create log table with the provided name on destination sql server.</param> | ||
/// <param name="columnOptions"></param> | ||
/// <param name="additionalColumns">Additional columns to be added</param> | ||
/// <returns>Logger configuration, allowing configuration to continue.</returns> | ||
/// <exception cref="T:System.ArgumentNullException">A required parameter is null.</exception> | ||
public static LoggerConfiguration MsSqlServerWithAdditionalColumns( | ||
this LoggerSinkConfiguration loggerConfiguration, | ||
string connectionString, | ||
string tableName, | ||
LogEventLevel restrictedToMinimumLevel = LogEventLevel.Verbose, | ||
int batchPostingLimit = 50, | ||
TimeSpan? period = null, | ||
IFormatProvider formatProvider = null, | ||
bool autoCreateSqlTable = false, | ||
ColumnOptions columnOptions = null, | ||
string schemaName = "dbo", | ||
Dictionary<string, Type> additionalColumns = null | ||
) | ||
{ | ||
if (columnOptions == null) | ||
{ | ||
columnOptions = new ColumnOptions(); | ||
} | ||
|
||
if (columnOptions.AdditionalDataColumns == null) | ||
{ | ||
columnOptions.AdditionalDataColumns = new List<System.Data.DataColumn>(); | ||
} | ||
|
||
if (additionalColumns != null) | ||
{ | ||
foreach (var columnName in additionalColumns.Keys) | ||
{ | ||
if (columnOptions.AdditionalDataColumns.Any(x => x.ColumnName.Equals(columnName))) | ||
{ | ||
continue; | ||
} | ||
columnOptions.AdditionalDataColumns.Add( | ||
new System.Data.DataColumn | ||
{ | ||
ColumnName = columnName, | ||
DataType = additionalColumns[columnName] | ||
}); | ||
} | ||
} | ||
|
||
return loggerConfiguration.MSSqlServer(connectionString, | ||
tableName, | ||
restrictedToMinimumLevel, | ||
batchPostingLimit, | ||
period, | ||
formatProvider, | ||
autoCreateSqlTable, | ||
columnOptions, | ||
schemaName); | ||
} | ||
|
||
/// <summary> | ||
/// Adds a sink that writes log events to a table in a MSSqlServer database. | ||
/// Adds a column named CorrelationId to the resulting table | ||
/// Create a database and execute the table creation script found here | ||
/// https://gist.github.com/mivano/10429656 | ||
/// or use the autoCreateSqlTable option. | ||
/// </summary> | ||
/// <param name="loggerConfiguration">The logger configuration.</param> | ||
/// <param name="connectionString">The connection string to the database where to store the events.</param> | ||
/// <param name="tableName">Name of the table to store the events in.</param> | ||
/// <param name="schemaName">Name of the schema for the table to store the data in. The default is 'dbo'.</param> | ||
/// <param name="restrictedToMinimumLevel">The minimum log event level required in order to write an event to the sink.</param> | ||
/// <param name="batchPostingLimit">The maximum number of events to post in a single batch.</param> | ||
/// <param name="period">The time to wait between checking for event batches.</param> | ||
/// <param name="formatProvider">Supplies culture-specific formatting information, or null.</param> | ||
/// <param name="autoCreateSqlTable">Create log table with the provided name on destination sql server.</param> | ||
/// <param name="columnOptions"></param> | ||
/// <param name="correlationId">CorrelationId parameter name. Default 'CorrelationId'</param> | ||
/// <param name="correlationIdType">Type of the correlationId parameter. Default is System.Guid</param> | ||
/// <returns>Logger configuration, allowing configuration to continue.</returns> | ||
/// <exception cref="T:System.ArgumentNullException">A required parameter is null.</exception> | ||
public static LoggerConfiguration MsSqlServerWithCorrelation( | ||
this LoggerSinkConfiguration loggerConfiguration, | ||
string connectionString, | ||
string tableName, | ||
LogEventLevel restrictedToMinimumLevel = LogEventLevel.Verbose, | ||
int batchPostingLimit = 50, | ||
TimeSpan? period = null, | ||
IFormatProvider formatProvider = null, | ||
bool autoCreateSqlTable = false, | ||
ColumnOptions columnOptions = null, | ||
string schemaName = "dbo", | ||
string correlationId = "CorrelationId", | ||
Type correlationIdType = null | ||
) | ||
{ | ||
if (columnOptions == null) | ||
{ | ||
columnOptions = new ColumnOptions(); | ||
} | ||
|
||
if (columnOptions.AdditionalDataColumns == null) | ||
{ | ||
columnOptions.AdditionalDataColumns = new List<System.Data.DataColumn>(); | ||
} | ||
|
||
if (!columnOptions.AdditionalDataColumns.Any(x => x.ColumnName.Equals(correlationId))) | ||
{ | ||
columnOptions.AdditionalDataColumns.Add( | ||
new System.Data.DataColumn | ||
{ | ||
ColumnName = correlationId, | ||
DataType = correlationIdType ?? typeof(Guid) | ||
}); | ||
} | ||
|
||
return loggerConfiguration.MSSqlServer(connectionString, | ||
tableName, | ||
restrictedToMinimumLevel, | ||
batchPostingLimit, | ||
period, | ||
formatProvider, | ||
autoCreateSqlTable, | ||
columnOptions, | ||
schemaName); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Correlation/NBB.Correlation.Serilog.SqlServer/NBB.Correlation.Serilog.SqlServer.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup Label="Globals"> | ||
<SccProjectName>SAK</SccProjectName> | ||
<SccProvider>SAK</SccProvider> | ||
<SccAuxPath>SAK</SccAuxPath> | ||
<SccLocalPath>SAK</SccLocalPath> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<Description>Extensions for creating new columns in sql Server for Serilog Table</Description> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Serilog" Version="$(SerilogPackageVersion)" /> | ||
<PackageReference Include="Serilog.Sinks.MSSqlServer" Version="$(SerilogSinksMSSqlServerPackageVersion)" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\NBB.Correlation\NBB.Correlation.csproj" /> | ||
</ItemGroup> | ||
|
||
|
||
</Project> |