Logging level transmutation

242 views
Skip to first unread message

Nicolas Tenoutasse

unread,
May 30, 2016, 6:41:59 AM5/30/16
to Serilog
Hello,

First, I have to say that I'm just discovering Serilog & Seq and that I'm pretty impressed!


Anyway, trying to use them in a MVC project, I have a few questions which I'm surprised that no one else seems to have addressed while they seem basic needs to me... But then maybe I missed something :-p

After using the 
          loggerFactory.AddSerilog();
The same logger is used jointly by MVC & my app but of course the level "information" from microsoft is much more verbose than I would like it so it would be nice if I could say that the information level from Microsoft is really my verbose level.

This has been somehow addressed a few days ago by https://github.com/serilog/serilog/issues/754 but it's not exatly the same... you propose to change the threshold while sometimes the comparative between two sources need to be leveld somehow.

Any idea of how can this be done?

Nicolas

nblum...@nblumhardt.com

unread,
May 30, 2016, 6:59:03 AM5/30/16
to Serilog
Hi Nicolas, thanks for getting in touch.

I think your proposal makes sense. There's no particular reason this isn't done today, only that the design space and the number of possible features are both huge so there's a reasonable amount of constraint put on what we attempt so that Serilog doesn't get bloated/haphazard.

Issue #754 linked above is working towards the same kind of goal - it'd be interesting to have your input on the GitHub ticket if you see this approach falling short.

Regards,
Nick

Ryan Farmer

unread,
Oct 3, 2016, 12:27:31 PM10/3/16
to Serilog
I had a similar issue, but in my case I wanted to filter based on the source that was logging info.  Some Microsoft logs were just not useful, so I wanted to include only the ones that were.  754 is probably a better overall solution, but my workaround for this in ASP.Net Core is to use a filter like so:
----------------
new LoggerConfiguration()
.Filter.ByExcluding(Serilog.Filters.Matching.WithProperty<Serilog.Events.ScalarValue>("SourceContext", scalar =>
{
    string sourceContext = scalar.Value as string;
    if (!string.IsNullOrEmpty(sourceContext))
    {
        if (sourceContext.StartsWith("Microsoft.AspNetCore."))
        {
            // We can have other conditionals here or use a dictionary to determine if we have a match
            // Useful mappings:
            if (sourceContext == "Microsoft.AspNetCore.Hosting.Internal.WebHost"
                || sourceContext == "Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker")
            {
                return false;
            }

            return true;
        }
    }

    return false;
}))
--------------------
You could also pass your own lambda and not use the scalar helper if you want the level, as well; it will pass a LogEvent to you and and you can choose whether or not to log it based on the SourceContext.  Not sure if that is set in MVC 5, though.

-Ryan
Reply all
Reply to author
Forward
0 new messages