|
| 1 | +using System.Collections.ObjectModel; |
| 2 | +using System.Data; |
| 3 | +using Serilog; |
| 4 | +using Serilog.Events; |
| 5 | +using Serilog.Sinks.MSSqlServer; |
| 6 | +using Serilog.Sinks.MSSqlServer.Sinks.MSSqlServer.Options; |
| 7 | + |
| 8 | +namespace NetStandardDemoLib |
| 9 | +{ |
| 10 | + public static class Initializer |
| 11 | + { |
| 12 | + private const string _connectionString = "Server=localhost;Database=LogTest;Integrated Security=SSPI;"; |
| 13 | + private const string _tableName = "LogEvents"; |
| 14 | + |
| 15 | + public static LoggerConfiguration CreateLoggerConfiguration() |
| 16 | + { |
| 17 | + return new LoggerConfiguration() |
| 18 | + .Enrich.FromLogContext() |
| 19 | + .WriteTo.MSSqlServer( |
| 20 | + _connectionString, |
| 21 | + new SinkOptions |
| 22 | + { |
| 23 | + TableName = _tableName, |
| 24 | + AutoCreateSqlTable = true |
| 25 | + }, |
| 26 | + sinkOptionsSection: null, |
| 27 | + appConfiguration: null, |
| 28 | + restrictedToMinimumLevel: LevelAlias.Minimum, |
| 29 | + formatProvider: null, |
| 30 | + columnOptions: BuildColumnOptions(), |
| 31 | + columnOptionsSection: null, |
| 32 | + logEventFormatter: null); |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + private static ColumnOptions BuildColumnOptions() |
| 37 | + { |
| 38 | + var columnOptions = new ColumnOptions |
| 39 | + { |
| 40 | + TimeStamp = |
| 41 | + { |
| 42 | + ColumnName = "TimeStampUTC", |
| 43 | + ConvertToUtc = true, |
| 44 | + }, |
| 45 | + |
| 46 | + AdditionalColumns = new Collection<SqlColumn> |
| 47 | + { |
| 48 | + new SqlColumn { DataType = SqlDbType.NVarChar, ColumnName = "MachineName" }, |
| 49 | + new SqlColumn { DataType = SqlDbType.NVarChar, ColumnName = "ProcessName" }, |
| 50 | + new SqlColumn { DataType = SqlDbType.NVarChar, ColumnName = "ThreadId" }, |
| 51 | + new SqlColumn { DataType = SqlDbType.NVarChar, ColumnName = "CallerName" }, |
| 52 | + new SqlColumn { DataType = SqlDbType.NVarChar, ColumnName = "SourceFile" }, |
| 53 | + new SqlColumn { DataType = SqlDbType.NVarChar, ColumnName = "LineNumber" } |
| 54 | + } |
| 55 | + }; |
| 56 | + |
| 57 | + columnOptions.Store.Remove(StandardColumn.Properties); |
| 58 | + |
| 59 | + return columnOptions; |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments