Files

159 lines
7.3 KiB
C#
Executable File

using Opsoft.NetCore.O3App.Domain;
using Opsoft.NetCore.O3App.Shared;
using System;
using System.Runtime.CompilerServices;
namespace Opsoft.NetCore.O3App.Infrastructure
{
public class SkynetLogger<T> : ILogger<T>
{
private readonly string module = "";
private readonly string category = "";
Microsoft.Extensions.Logging.ILoggerFactory loggerFactory;
private readonly ITraceContextAccessor traceContextAccessor;
SerilogService logService;
public SkynetLogger(Microsoft.Extensions.Logging.ILoggerFactory loggerFactory, ITraceContextAccessor traceContextAccessor)
{
this.loggerFactory = Guard.ArgumentNotNull(loggerFactory, nameof(loggerFactory));
this.traceContextAccessor = Guard.ArgumentNotNull(traceContextAccessor, nameof(traceContextAccessor));
var type = typeof(T);
this.module = type.Namespace;
this.category = type.Name;
this.logService = new SerilogService(loggerFactory.CreateLogger(module));
}
public void Debug(string message, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Debug(Format(message, module, category, callerName, filter1, filter2));
}
public void Debug(object messageObject, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Debug(Format(messageObject, module, category, callerName, filter1, filter2));
}
public void Error(string message, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Error(Format(message, module, category, callerName, filter1, filter2));
}
public void Error(object messageObject, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Error(Format(messageObject, module, category, callerName, filter1, filter2));
}
public void Error(Exception ex, string message, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Error(ex, Format(message, module, category, callerName, filter1, filter2));
}
public void Error(Exception ex, object messageObject, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Error(ex, Format(messageObject, module, category, callerName, filter1, filter2));
}
public void Fatal(string message, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Fatal(Format(message, module, category, callerName, filter1, filter2));
}
public void Fatal(object messageObject, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Fatal(Format(messageObject, module, category, callerName, filter1, filter2));
}
public void Fatal(Exception ex, string message, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Fatal(ex, Format(message, module, category, callerName, filter1, filter2));
}
public void Fatal(Exception ex, object messageObject, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Fatal(ex, Format(messageObject, module, category, callerName, filter1, filter2));
}
public void Info(string message, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Info(Format(message, module, category, callerName, filter1, filter2));
}
public void Info(object messageObject, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Info(Format(messageObject, module, category, callerName, filter1, filter2));
}
public void Warn(string message, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Warn(Format(message, module, category, callerName, filter1, filter2));
}
public void Warn(object messageObject, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Warn(Format(messageObject, module, category, callerName, filter1, filter2));
}
public void Warn(Exception ex, object messageObject, string filter1 = "", string filter2 = "", [CallerMemberName] string callerName = "")
{
logService.Warn(ex, Format(messageObject, module, category, callerName, filter1, filter2));
}
private string Format(object message, string module = null, string category = null, string subCategory = null, string filter1 = null, string filter2 = null)
{
string messageText = message.ToString();
if (message.GetType().IsComplexType())
{
messageText = message.ToJson(new Newtonsoft.Json.JsonSerializerSettings()
{
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
});
}
string traceId = traceContextAccessor.TraceContext?.TraceId ?? string.Empty;
if (traceId.NotNullOrWhiteSpace())
{
traceId = $"$apmTxId:{traceId}$";
}
return $"{traceId} [{module}][{category}][{subCategory}][{filter1}][{filter2}]{messageText}".TrimStart();
}
public void Info(string module, string category, string subCategory, string logContent, string filter1, string filter2)
{
logService.Info(Format(logContent, module, category, subCategory, filter1, filter2));
}
public void Error(string module, string category, string subCategory, string logContent, string filter1, string filter2)
{
logService.Error(Format(logContent, module, category, subCategory, filter1, filter2));
}
class SerilogService
{
Microsoft.Extensions.Logging.ILogger logger;
public SerilogService(Microsoft.Extensions.Logging.ILogger logger)
{
this.logger = Guard.ArgumentNotNull(logger, nameof(logger));
}
internal void Debug(string v) => Microsoft.Extensions.Logging.LoggerExtensions.LogDebug(logger, v);
internal void Error(string v) => Microsoft.Extensions.Logging.LoggerExtensions.LogError(logger, v);
internal void Error(Exception ex, string v) => Microsoft.Extensions.Logging.LoggerExtensions.LogError(logger, ex, v);
internal void Fatal(string v) => Microsoft.Extensions.Logging.LoggerExtensions.LogCritical(logger, v);
internal void Fatal(Exception ex, string v) => Microsoft.Extensions.Logging.LoggerExtensions.LogCritical(logger, ex, v);
internal void Info(string v) => Microsoft.Extensions.Logging.LoggerExtensions.LogInformation(logger, v);
internal void Warn(string v) => Microsoft.Extensions.Logging.LoggerExtensions.LogWarning(logger, v);
internal void Warn(Exception ex, string v) => Microsoft.Extensions.Logging.LoggerExtensions.LogWarning(logger, ex, v);
}
}
}