I am having issue, when using multiple contexts to log to different files with Serilog
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
.WriteTo.Logger(req => req
.Filter.ByIncludingOnly(LoggerHelper.IsRequest)
.WriteTo.File($"{Configuration.GetSection("Serilog").GetSection("LogLocationFile").Value}.Requests.log", rollingInterval: RollingInterval.Day))
.WriteTo.Logger(req => req
.Filter.ByIncludingOnly(LoggerHelper.IsException)
.WriteTo.File($"{Configuration.GetSection("Serilog").GetSection("LogLocationFile").Value}.Exceptions.log", rollingInterval: RollingInterval.Day))
.WriteTo.Logger(log => log
.Filter.ByExcluding(e => LoggerHelper.IsRequest(e) || LoggerHelper.IsException(e))
.WriteTo.File($"{Configuration.GetSection("Serilog").GetSection("LogLocationFile").Value}.log", rollingInterval: RollingInterval.Day))
.CreateLogger();
For example I am using following lines to log:
Log.ForContext(LoggerHelper.RequestContext, "").Information(MessageTemplate, httpContext.Request.Method, httpContext.Request.Path,
statusCode, sw.Elapsed.TotalMilliseconds);
Log.ForContext(LoggerHelper.ExceptionContext, "").Error(MessageTemplate, "Exception", error.Code,
error.Message,
context?.User?.Claims.FirstOrDefault(x => x.Type == Claims.ClaimInternalId)?.Value ??
Guid.Empty.ToString());
LoggerHelper class as following:
public static class LoggerHelper
{
public static readonly string RequestContext = "Request";
public static readonly string ExceptionContext = "Exception";
public static Func<LogEvent, bool> IsRequest = Matching.FromSource(RequestContext);
public static Func<LogEvent, bool> IsException = Matching.FromSource(ExceptionContext);
}
When I had only 1 context, the .ByExcluding(LoggerHelper.IsRequest) worked correctly, but now it only logs everything to 1 file, when it should have 3 different files.
Has anyone encountered similar issue or am I missing something really simple?
LoggerHelpermethods and properties - that's where the problem would be. Impossible to help more without knowing what the code forLoggerHelperis