1

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?

3
  • The filters are applied based on the custom code you wrote on the LoggerHelper methods and properties - that's where the problem would be. Impossible to help more without knowing what the code for LoggerHelper is Commented Aug 11, 2021 at 15:01
  • I added my LoggerHelper, it's quite straight forward, since it only should check if source is matching. Commented Aug 11, 2021 at 15:05
  • Turns out I did overlooked simple thing ... ^^ Commented Aug 11, 2021 at 15:20

1 Answer 1

2

Turns out I used wrong method in this case. I need to use Matching.WithProperty(e) instead of Matching.FromSource(e) and this fixed the issue I was having.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.