-
Notifications
You must be signed in to change notification settings - Fork 5
/
SeriLogger.cs
90 lines (76 loc) · 3.76 KB
/
SeriLogger.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using Logging.Models;
using Microsoft.Extensions.Configuration;
using Serilog;
using Serilog.Core;
using Serilog.Debugging;
using Serilog.Events;
using Serilog.Sinks.MSSqlServer;
using System;
using System.Collections.ObjectModel;
using System.Data;
using System.IO;
namespace Logging
{
public abstract class SeriLogger : IDisposable
{
private readonly string connectionString;
private const string configSql = "Serilog:Sql";
protected const string configTable = configSql + ":TableName";
protected const string configModuleStatus = "Serilog:Modules";
protected readonly ILogger logger;
protected readonly IConfiguration configuration;
protected abstract string TableName { get; }
public SeriLogger(IConfiguration configuration)
{
this.configuration = configuration;
connectionString = configuration.GetConnectionString("LoggingConnectionString");
logger = new LoggerConfiguration()
.WriteTo.MSSqlServer(
connectionString: connectionString,
schemaName: configuration[$"{configSql}:SchemaName"],
tableName: TableName,
autoCreateSqlTable: Convert.ToBoolean(configuration[$"{configSql}:AutoCreateSqlTable"]),
columnOptions: GetColumnOptions(),
restrictedToMinimumLevel: (LogEventLevel)Convert.ToInt32(configuration[$"{configSql}:RestrictedToMinimumLevel"]),
batchPostingLimit: Convert.ToInt32(configuration[$"{configSql}:BatchPostingLimit"]),
period: TimeSpan.Parse(configuration[$"{configSql}:Period"]))
.CreateLogger();
if (Convert.ToBoolean(configuration[$"Serilog:SelfLog"]))
{
SelfLog.Enable(message =>
{
var directoryPath = $@"Serilog\{TableName}";
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
File.AppendAllText($"{directoryPath}\\SerilogDebug_{DateTime.Now.ToFileTimeUtc()}.txt", message);
});
}
}
protected virtual ColumnOptions GetColumnOptions()
{
var options = new ColumnOptions();
options.DisableTriggers = true;
options.Store.Clear();
options.AdditionalColumns = new Collection<SqlColumn>
{
new SqlColumn {ColumnName = nameof(LogDetail.Timestamp), DataType = SqlDbType.DateTime, AllowNull = false},
new SqlColumn {ColumnName = nameof(LogDetail.Product), DataType = SqlDbType.NVarChar, DataLength = 100},
new SqlColumn {ColumnName = nameof(LogDetail.Message), DataType = SqlDbType.NVarChar},
new SqlColumn {ColumnName = nameof(LogDetail.UserId), DataType = SqlDbType.Int},
new SqlColumn {ColumnName = nameof(LogDetail.UserName), DataType = SqlDbType.NVarChar, DataLength = 200},
new SqlColumn {ColumnName = nameof(LogDetail.Location), DataType = SqlDbType.NVarChar, DataLength = 500},
new SqlColumn {ColumnName = nameof(LogDetail.Hostname), DataType = SqlDbType.NVarChar, DataLength = 100},
new SqlColumn {ColumnName = nameof(LogDetail.SessionId), DataType = SqlDbType.VarChar, DataLength = 100},
new SqlColumn {ColumnName = nameof(LogDetail.CorrelationId), DataType = SqlDbType.VarChar, DataLength = 100},
new SqlColumn {ColumnName = nameof(LogDetail.AdditionalInfo), DataType = SqlDbType.NVarChar},
};
return options;
}
public void Dispose()
{
(logger as Logger)?.Dispose();
}
}
}