Skip to content

Commit

Permalink
Merge pull request #22 from osstotalsoft/feature/serilog-extensions
Browse files Browse the repository at this point in the history
serilog extensions
  • Loading branch information
VCuzmin authored Aug 14, 2019
2 parents b2d58e6 + e7355b4 commit f2f8861
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 2 deletions.
11 changes: 9 additions & 2 deletions NBB.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28803.156
# Visual Studio 15
VisualStudioVersion = 15.0.28307.757
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{7311E32F-C1B0-41C9-B5F1-DE9EBB6ABB55}"
EndProject
Expand Down Expand Up @@ -292,6 +292,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".github", ".github", "{F577
.github\release-drafter.yml = .github\release-drafter.yml
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NBB.Correlation.Serilog.SqlServer", "src\Correlation\NBB.Correlation.Serilog.SqlServer\NBB.Correlation.Serilog.SqlServer.csproj", "{F442DC3C-D2E9-468F-A6E3-D639E6F5D168}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -664,6 +666,10 @@ Global
{9DDF53B1-7728-4CF3-BEBE-2057A6956F59}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9DDF53B1-7728-4CF3-BEBE-2057A6956F59}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9DDF53B1-7728-4CF3-BEBE-2057A6956F59}.Release|Any CPU.Build.0 = Release|Any CPU
{F442DC3C-D2E9-468F-A6E3-D639E6F5D168}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F442DC3C-D2E9-468F-A6E3-D639E6F5D168}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F442DC3C-D2E9-468F-A6E3-D639E6F5D168}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F442DC3C-D2E9-468F-A6E3-D639E6F5D168}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -796,6 +802,7 @@ Global
{48E5BC6A-5846-4AD7-BFFB-C1D04C5C1BC2} = {70EA3420-74E3-4C9D-9858-57EC01AD31B3}
{9DDF53B1-7728-4CF3-BEBE-2057A6956F59} = {48E5BC6A-5846-4AD7-BFFB-C1D04C5C1BC2}
{F57779EB-9107-427D-A65C-5AA262C348E1} = {EB7B0468-DE25-4350-80AD-50E7C4D09120}
{F442DC3C-D2E9-468F-A6E3-D639E6F5D168} = {307E1650-6128-4A36-9C98-297F2BF71C83}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {23A42379-616A-43EF-99BC-803DF151F54E}
Expand Down
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);
}
}
}
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>

0 comments on commit f2f8861

Please sign in to comment.