-
Notifications
You must be signed in to change notification settings - Fork 5
/
PerformanceLogger.cs
56 lines (47 loc) · 2.06 KB
/
PerformanceLogger.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
using Logging.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Serilog.Sinks.MSSqlServer;
using System;
using System.Collections.Generic;
using System.Data;
namespace Logging
{
public class PerformanceLogger : SeriLogger
{
private readonly IHttpContextAccessor contextAccessor;
protected override string TableName => configuration[$"{configTable}:Performance"];
public PerformanceLogger(IConfiguration configuration, IHttpContextAccessor contextAccessor)
: base(configuration)
{
this.contextAccessor = contextAccessor;
}
public void Log(string message, long? elapsedMilliseconds, Dictionary<string, object> additionalInfo)
{
if (!Convert.ToBoolean(configuration[$"{configModuleStatus}:Performance"]))
{
return;
}
var logDetail = new PerformanceLogDetail();
logDetail.PopulateModel(message, contextAccessor.HttpContext, configuration, additionalInfo, true);
logDetail.ElapsedMilliseconds = elapsedMilliseconds;
logger.Information("{Timestamp}{Message}{Location}{Product}" +
"{Hostname}" +
"{UserId}{UserName}{SessionId}{CorrelationId}{AdditionalInfo}{ElapsedMilliseconds}",
logDetail.Timestamp, logDetail.Message,
logDetail.Location,
logDetail.Product,
logDetail.Hostname, logDetail.UserId,
logDetail.UserName, logDetail.SessionId,
logDetail.CorrelationId,
logDetail.AdditionalInfoDictionary.JsonSerialize(),
logDetail.ElapsedMilliseconds);
}
protected override ColumnOptions GetColumnOptions()
{
var options = base.GetColumnOptions();
options.AdditionalColumns.Add(new SqlColumn { ColumnName = nameof(PerformanceLogDetail.ElapsedMilliseconds), DataType = SqlDbType.BigInt });
return options;
}
}
}