Created
June 25, 2021 01:15
-
-
Save MahdiKarimipour/1136cc108dd3cf432ce37d842513ce9e to your computer and use it in GitHub Desktop.
ILogger Full Implementation
This file contains hidden or 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
public class NLogger : Microsoft.Extensions.Logging.ILogger | |
{ | |
private readonly Logger logger; | |
private readonly string name; | |
private readonly NloggerConfiguration nloggerConfiguration; | |
private readonly UserContext userContext; | |
private readonly RequestContext requestContext; | |
public NLogger(string name, | |
NloggerConfiguration nloggerConfiguration, | |
UserContext userContext, | |
RequestContext requestContext) | |
{ | |
this.name = name; | |
this.nloggerConfiguration = nloggerConfiguration; | |
this.userContext = userContext; | |
this.requestContext = requestContext; | |
logger = NLogBuilder | |
.ConfigureNLog(GetLogConfigFileName()) | |
.GetCurrentClassLogger(); | |
logger.Factory.Flush(TimeSpan.FromSeconds(1000)); | |
} | |
public IDisposable BeginScope<TState>(TState state) | |
{ | |
return null; | |
} | |
public bool IsEnabled(Microsoft.Extensions.Logging.LogLevel logLevel) | |
{ | |
return true; | |
} | |
public void Log<TState>( | |
Microsoft.Extensions.Logging.LogLevel logLevel, | |
EventId eventId, | |
TState state, | |
Exception exception, | |
Func<TState, Exception, string> formatter) | |
{ | |
if (!IsEnabled(logLevel)) | |
{ | |
return; | |
} | |
if (nloggerConfiguration.EventId == 0 || nloggerConfiguration.EventId == eventId.Id) | |
{ | |
var logEventInfo = new LogEventInfo(GetLogLevel(logLevel), name, $"{formatter(state, exception)}"); | |
if (!exception.IsEmpty()) | |
{ | |
logEventInfo.Exception = exception; | |
} | |
logger | |
.WithProperty("UserId", userContext.UserId) | |
.WithProperty("Environment", Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")) | |
.WithProperty("Channel", userContext.Channel) | |
.WithProperty("UserAgent", requestContext.UserAgent) | |
.WithProperty("CorrelationId", requestContext.CorrelationId) | |
.WithProperty("EventId", eventId.Id) | |
.Log(logEventInfo); | |
} | |
} | |
public static string GetLogConfigFileName() | |
{ | |
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") | |
.Equals(Constants.Environment.Production, | |
System.StringComparison.InvariantCultureIgnoreCase)) | |
{ | |
return "nlog.production.config"; | |
} | |
return "nlog.config"; | |
} | |
internal NLog.LogLevel GetLogLevel(Microsoft.Extensions.Logging.LogLevel level) | |
{ | |
switch (level) | |
{ | |
case Microsoft.Extensions.Logging.LogLevel.Trace: | |
return NLog.LogLevel.Trace; | |
case Microsoft.Extensions.Logging.LogLevel.Debug: | |
return NLog.LogLevel.Debug; | |
case Microsoft.Extensions.Logging.LogLevel.Information: | |
return NLog.LogLevel.Info; | |
case Microsoft.Extensions.Logging.LogLevel.Warning: | |
return NLog.LogLevel.Warn; | |
case Microsoft.Extensions.Logging.LogLevel.Error: | |
return NLog.LogLevel.Error; | |
case Microsoft.Extensions.Logging.LogLevel.Critical: | |
return NLog.LogLevel.Fatal; | |
} | |
return NLog.LogLevel.Info; | |
} | |
} | |
public class NloggerConfiguration | |
{ | |
public LogLevel LogLevel { get; set; } = LogLevel.Trace; | |
public int EventId { get; set; } = 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment