Serilog: restrictedToMinimumLevel using LevelSwitches - c#

I am using Serilog in a .Net 5 application, and I am configuring it loading the configuration from appsettings.{env}.json.
In this file I want to specify a different minimum logging level for each sink, and I would like to control it with a LevelSwitch so that I don't need to restart my application only for changing this. To achieve this, I know that restrictedToMinimumLevel can be specified for each sink, but I cannot find an example where it is controlled by a switch. It seems it can only be a string with the logging level, since when I tried to bind it to a switch I got this exception:
System.InvalidOperationException: 'Cannot create instance of type 'Serilog.Events.LogEventLevel' because it is missing a public parameterless constructor.'
Am I missing something? How can I set the minimum logging level for each sink to be easily changed at runtime without restarting my application?
These are my settings:
{
"Serilog": {
"Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File" ],
"LevelSwitches": {
"$baseSwitch": "Verbose",
"$consoleSwitch": "Warning"
},
"MinimumLevel": {
"ControlledBy": "$baseSwitch"
},
"WriteTo": [
{
"Name": "Console",
"Args": {
//"restrictedToMinimumLevel": "Warning", ---> this works
//"restrictedToMinimumLevel": { ---> this is what I need, but doesn't work
// "ControlledBy": "$consoleSwitch"
//},
"outputTemplate": "{Timestamp} [{Level:u3}] [{LogSource}]: {Message} {Properties}{NewLine}{Exception}"
}
},
{
"Name": "File",
"Args": {
"path": "Logs/log.txt"
}
}
],
"Enrich": [
"FromLogContext"
]
}
}
I am open also to other solutions. Thank you.

The name of the level switch reference should not have the $ prefix. The $ prefix is only for referencing the level switch declared
{
"Serilog": {
"LevelSwitches": { "consoleSwitch": "Verbose" }, <<<< No $ prefix
"WriteTo": [
{
"Name": "Console",
"Args": {
"levelSwitch": "$consoleSwitch" <<<<<< $ prefix
}
}
]
}
}
Also, the name of the argument for the level switch has to match what's declared in the extension method for the sink. In the case of the Console sink, it's called levelSwitch.

Related

Nlog with Npgsql throws NLog.NLogConfigurationException: 'DatabaseParameterInfo' cannot assign unknown property 'parameterName'='#Exception'

my code is throwing me expcetion Unhandled exception. NLog.NLogConfigurationException: 'DatabaseParameterInfo' cannot assign unknown property 'parameterName'='#Exception'
I'm using NLog package (5.1.0) in my ASP.NET WEB API project.
My database is PostgreSQL so I use Npgsql (Npgsql.EntityFrameworkCore.PostgreSQL and transitive package Npgsql). My configuration in appsettings.json looks like
...
"NLog": {
"throwConfigExceptions": true,
"autoReload": true,
"extensions": [
{ "assembly": "NLog.Extensions.Logging" },
{ "assembly": "NLog.Web.AspNetCore" }
],
"targets": {
"async": true,
"console": {
"type": "Console"
},
"database": {
"type": "Database",
"dbProvider": "Npgsql.NpgsqlConnection, Npgsql",
"connectionString": "",
"commandText": "INSERT INTO Logs (Exception) VALUES (#Exception)",
"parameters": [
{
"parameterName": "#Exception",
"layout": "${exception:tostring}"
}
]
}
},
"rules": [
{
"logger": "*",
"minLevel": "Trace",
"writeTo": "console"
},
{
"logger": "*",
"minLevel": "Error",
"writeTo": "database"
}
]
},
...
I don't set the connection string in the application settings because I want to do it programmatically in program.cs.
my program.cs looks like
...
var builder = WebApplication.CreateBuilder(args);
var configuration = builder.configuration;
var dbConnectionOptions = new DbConnectionOptions(configuration);
configuration.GetSection("NLog")
.GetSection("targets")
.GetSection("database")
.GetSection("connectionString").Value = dbConnectionOptions.AppDb.ConnectionString;
...
In Npgsql specification https://www.npgsql.org/doc/basic-usage.html#parameters I see, that there are two ways of defining parameters. One is using $ and another is using # but nothing seems to work.
Does anyone know what can I change to fix it?
I've tried setting commandText Values as #Exception, $Exception, Exception, %Exception, and setting parameterName as #Exception, $Exception, Exception, %Exception (mixing all options), but always got same result.
NLog complains about not recognizing the property parameterName.
Try replacing:
"parameterName": "#Exception",
With:
"name": "#Exception",
See also: https://github.com/NLog/NLog.Extensions.Logging/wiki/NLog-configuration-with-appsettings.json
Also notice that NLog v5 requires NLog.Database-nuget-package for the NLog DatabaseTarget to work.

Serilog Filtering not working in appsettings.json

I implemented Serilogger and put the configuration in the Program.cs and it worked great. I'm trying to move the configuration to appsettings.json and its writing to the logs but I can't get the filters to work now. I know its reading the config because I can change the minimum log level and I see the result in the logs.
I've been reading and rereading the Serilog pages in Github, especially the configuration page
Here is the code that worked in Program.cs
try
{
Serilog.Debugging.SelfLog.Enable(msg => Debug.WriteLine(msg));
var levelSwitch = new LoggingLevelSwitch();
levelSwitch.MinimumLevel = LogEventLevel.Warning;
return new LoggerConfiguration()
.ReadFrom.Configuration(config, "Serilog");
.MinimumLevel.ControlledBy(levelSwitch)
.Enrich.FromLogContext()
.WriteTo.Debug()
.WriteTo.Logger(l => l
.Filter.ByExcluding("source = 'application'")
.WriteTo.File("Logs\\log-.txt", rollingInterval: RollingInterval.Day))
.WriteTo.Logger(l => l
.Filter.ByIncludingOnly("source='customer'")
.WriteTo.MSSqlServer(
connectionString:
"Server=(localdb)\\Infinity;Database=Infinity;Trusted_Connection=True;MultipleActiveResultSets=True;",
sqlSinkOptions));
}
Here is the new appsettings file
{
"Serilog": {
"Using": [ "Serilog.Enrichers.FromLogContext" ],
"MinimumLevel": "Warning",
"WriteTo": [
{
"Name": "MSSqlServer",
"Args": {
"connectionString": "Server=(localdb)\\Infinity;Database=Infinity;Trusted_Connection=True;MultipleActiveResultSets=True;",
"tableName": "Log",
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "source = 'customer'"
}
}
]
}
},
{
"Name": "File",
"Args": {
"path": "Logs\\log-.txt",
"rollingInterval": "Day",
"retainedFileCountLimit": 7,
"Filter": [
{
"Name": "ByExcluding",
"Args": {
"expression": "source = 'customer'"
}
}
]
}
}
],
"Enrich": [ "FromLogContext" ]
},
Update: I figured out the custom property that I'm filtering on is not getting inserted into the logger object any longer. I think that has something to do with the "enricher"? But its not the source of the filter problem, because the "IncludeOnly" filter shouldn't write anything if the property isn't there, correct?
Here is a snippet from the method where I'm using PushProperty to insert the property and value. Since I haven't touched this code, I assume the problem is still in the app settings.
public void LogError(LogDestination destination, LogLevel level, string msg)
{
if (destination == LogDestination.Customer)
{
using (LogContext.PushProperty("source", "customer")) {
switch (level)
{
case LogLevel.Debug:
_logger.LogInformation(msg);
break;
I've also tried adding the .Enrich back into the LoggerConfiguration with no difference.
return new LoggerConfiguration()
.ReadFrom.Configuration(config, "Serilog")
.Enrich.FromLogContext();
Update I added a global filter at the top-level. I tried an Include Only and Exclude and both work as expected. I can't figure out why the sink filters aren't working.
{
"Serilog": {
"Using": [ "Serilog.Expressions", "Serilog.Sinks.File", "Serilog.Sinks.MSSqlServer" ],
"MinimumLevel": "Warning",
"Filter": [
{
"Name": "ByExluding",
"Args": {
"expression": "source = 'customer'"
}
}
],
I have tried using Filters instead of Filter in Appsettings file, it's working for me. But didn't see any documentation around it.
{
"Serilog": {
"Using": [ "Serilog.Expressions", "Serilog.Sinks.File", "Serilog.Sinks.MSSqlServer" ],
"MinimumLevel": "Warning",
"Filters": [
{
"Name": "ByExluding",
"Args": {
"expression": "source = 'customer'"
}
}
],

How to enable multiple files with Serilog RollingFile sink?

I've setup my sink as follows:
"WriteTo": [
{
"Name": "RollingFile",
"Args": {
"pathFormat": "log-{Date}.log",
"fileSizeLimitBytes": 20000000,
}
}
]
My understanding was that once the log-06042019.log file reaches 20000000 bytes, it will start logging to log-06042019-001.log, then to log-06042019-002.log and so on.
But that doesn't happen. It simply stops logging until the next day.
Am I missing something simple in order to enable the rolling characteristics of this sink?
You shouldn't use the RollingFile sink anymore. Instead, use the File Sink.
The file sink has a setting to roll over at a certain size. Here's the C# configuration:
.WriteTo.File("log.txt", rollOnFileSizeLimit: true)
or appsettings.json:
{
"Serilog": {
"WriteTo": [
{ "Name": "File", "Args": { "path": "log.txt", "rollOnFileSizeLimit": "true" } }
]
}
}

Different Minimum Level Logs Serilog

Is there a way to differentiate what level is logged between the different loggers for Serilog? I want to be able to log MinimumLevel Debug to the console output but only Warning and above to my file output. I am using ASP.NET Core 2.1 and this is what the appsetting.json currently looks like:
"Serilog": {
"Using": [ "Serilog.Sinks.Console" ],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "RollingFile",
"IsJson": true,
"Args": {
"pathFormat": "C:\\Logs\\Log-{Hour}.json",
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
}
},
{
"Name": "Console"
}
]
},
Is it something like another parameter under "Args"? I've tried "minimumnLevel" in this location but it did not work.
The setting you're looking for is restrictedToMinimumLevel. This GitHub issue shows some examples of this, but for your example, you just need to add restrictedToMinimumLevel to your Args for RollingFile:
"Serilog": {
"Using": [ "Serilog.Sinks.Console" ],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "RollingFile",
"IsJson": true,
"Args": {
"pathFormat": "C:\\Logs\\Log-{Hour}.json",
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog",
"restrictedToMinimumLevel": "Warning"
}
},
{
"Name": "Console"
}
]
},
Working from the answers above, this is how I set it up in code as opposed to config
LoggerConfiguration GetConfig()
=> new LoggerConfiguration()
.WriteTo.Seq(serverUrl: "http://localhost:5341", restrictedToMinimumLevel: LogEventLevel.Debug)
.WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Error);
var logger = GetConfig().CreateLogger();
builder.RegisterInstance(logger).As<ILogger>();
After I did this, it worked for log level Information and above. Debug didn't work. This post explained why.
I had to set a 'global' minimum level like this.
LoggerConfiguration GetConfig()
=> new LoggerConfiguration()
.WriteTo.Seq(serverUrl: "http://localhost:5341", restrictedToMinimumLevel: LogEventLevel.Debug)
.WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Error)
.MinimumLevel.Verbose();
In your configuration you have one Serilog logger, but you have 2 sinks. One of your sinks is RollingFile and the other is Console.
You can override (but only raise) the minimum logging level per sink, The argument is called restrictedToMinimumLevel.
Since you want to raise the minimum logging level from your logger's default Debug to Warning in your file sink, in your appsettings.json file, it would look like this:
"Serilog": {
"Using": [ "Serilog.Sinks.Console" ],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "RollingFile",
"IsJson": true,
"Args": {
"pathFormat": "C:\\Logs\\Log-{Hour}.json",
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog",
"restrictedToMinimumLevel": "Warning"
}
},
{
"Name": "Console"
}
]
},

Filter Serilog logs to different sinks depending on context source?

I have a .NET Core 2.0 application in which I successfully use Serilog for logging. Now, I would like to log some database performance statistics to a separate sink (they are not for debugging, which basically is the purpose of all other logging in the application, so I would like to keep them separate) and figured this could be accomplished by creating the DB statistics logger with Log.ForContext<MyClass>().
I do not know how I am supposed to configure Serilog using my appsettings.json to log my "debug logs" to one sink and my DB statistics log to another? I am hoping it is possible to do something like:
"Serilog": {
"WriteTo": [
{
"Name": "RollingFile",
"pathFormat": "logs/Log-{Date}.log",
"Filter": {
"ByExcluding": "FromSource(MyClass)"
}
},
{
"Name": "RollingFile",
"pathFormat": "logs/DBStat-{Date}.log",
"Filter": {
"ByIncludingOnly": "FromSource(MyClass)"
}
}
]
}
The "Filter" parts of the configuration is pure guesswork on my part. Is this possible using my configuration filer or do I need to do this in code in my Startup.cs file?
EDIT: I have got it working using the C# API but would still like to figure it out using JSON config:
Log.Logger = new LoggerConfiguration()
.WriteTo.Logger(lc => lc
.Filter.ByExcluding(Matching.FromSource<MyClass>())
.WriteTo.LiterateConsole())
.WriteTo.Logger(lc => lc
.Filter.ByExcluding(Matching.FromSource<MyClass>())
.WriteTo.RollingFile("logs/DebugLog-{Date}.log"))
.WriteTo.Logger(lc => lc
.Filter.ByIncludingOnly(Matching.FromSource<MyClass>())
.WriteTo.RollingFile("logs/DBStats-{Date}.log", outputTemplate: "{Message}{NewLine}"))
.CreateLogger();
I completed this work today, and thought that I'd provide a proper answer since it took me quite a few posts, issues and other pages to work through to get this sorted out.
It's useful to have all the logs, but I also wanted to log only my API code separately, and omit the Microsoft. namespace logs. The JSON config to do that looks like this:
"Serilog": {
"Using": [ "Serilog.Sinks.File" ],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "/var/logs/system.log",
... //other unrelated file config
}
},
{
"Name": "Logger",
"Args": {
"configureLogger": {
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "/var/logs/api.log",
... //other unrelated file config
}
}
],
"Filter": [
{
"Name": "ByExcluding",
"Args": {
"expression": "StartsWith(SourceContext, 'Microsoft.')"
}
}
]
}
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ]
... //Destructure and other config
}
The top-level WriteTo is the first simple, global, sink. All log events write to this. If you add a Filter section on the same level as this, it will affect all configured WriteTo elements.
Then I configure another WriteTo as a Logger (not File), but the Args for this looks different and has a configureLogger element which serves the same purpose as Serilog on the top level, that is to say, it is the top level of the sub-logger. This means that you can easily split out the config for this into a separate file and add it additionally in the config builder (see bottom).
From here, this sub-logger works the same way: You can configure multiple WriteTos, and the Filter element on this level will affect only this sub-logger.
Simply add more "Name": "Logger" elements to the top level WriteTo section and setup filters for each one separately.
Note
It is also important to note that, even though you are doing all this in config and not referencing a single bit of the Serilog.Filters.Expressions package in your code, you still have to add the NuGet reference to that package. It doesn't work without the package reference.
About splitting the config:
If I have to add more loggers, I would definitely split out the different loggers into separate files for clarity, e.g.
appsettings.json:
"Serilog": {
"Using": [ "Serilog.Sinks.File" ],
"MinimumLevel": "Error",
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "/var/logs/system.log",
...
}
},
{
"Name": "Logger",
"Args": {
"configureLogger": {} // leave this empty
}
}
],
"Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ],
...
apilogger.json:
{
"Serilog:WriteTo:1:Args:configureLogger": { //notice this key
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "/var/logs/api_separateFile.log",
...
}
}
],
"Filter": [
{
"Name": "ByExcluding",
"Args": {
"expression": "StartsWith(SourceContext, 'Microsoft.')"
}
}
]
}
}
And then adjust my IWebHost builder to include the additional config:
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddJsonFile("apilogger.json", optional: false, reloadOnChange: false);
})
.UseStartup<Startup>();
This way it is easier to understand, read and maintain.
I had to do a similar thing, but in code, not JSON. Using a logger as a sink, as described at the bottom of https://github.com/serilog/serilog/wiki/Configuration-Basics did the trick.
In my case I wanted to log everything to a file and to the console, except that messages from a specific source should go only to the file:
private static bool SourceContextEquals(LogEvent logEvent, Type sourceContext)
=> logEvent.Properties.GetValueOrDefault("SourceContext") is ScalarValue sv && sv.Value?.ToString() == sourceContext.FullName;
private static ILogger CreateLogger() =>
new LoggerConfiguration()
.WriteTo.File("mylog.log")
.WriteTo.Logger(lc =>
lc.Filter.ByExcluding(le => SourceContextEquals(le, typeof(TypeThatShouldLogOnlyToFile)))
.WriteTo.Console()
)
.CreateLogger();
Writing only entityframework log to a file is done below but we need to install Serilog.Filters.Expressions nuget package
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Warning",
"System": "Warning",
"Microsoft.EntityFrameworkCore": "Information"
}
},
"WriteTo": [
{
"Name": "Logger",
"Args": {
"configureLogger": {
"Filter": [
{
"Name": "ByIncludingOnly",
"Args": {
"expression": "StartsWith(SourceContext, 'Microsoft.EntityFrameworkCore')"
}
}
],
"WriteTo": [
{
"Name": "File",
"Args": {
"path": "C:\\LOGS\\TestService_EF_.json",
"outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}",
"rollingInterval": "Day",
"retainedFileCountLimit": 7
}
}
]
}
}
}
]
}

Categories