4

Is there a way to do this? I would like to change the minimum level of logging by reading the setting off the appsettings.json

My code looks like this

Serilog.Log.Logger = new LoggerConfiguration()
    .Enrich.WithExceptionDetails()
    .MinimumLevel.Verbose()
    .WriteTo.Console()
    .Enrich.WithProperty("Application", applicationName)
    .WriteTo.File(new JsonFormatter(renderMessage: true), "logs/myapp.txt", rollingInterval: RollingInterval.Day)
    .CreateLogger();
7
  • 1
    Well, what have you tried? Have you read the docs? Commented Oct 27, 2021 at 16:56
  • I know how to read the value off of the appsettings.json file - but i am unsure how to pass it in, since it is not an enum Commented Oct 27, 2021 at 17:01
  • 1
    This should have everything you need. nblumhardt.com/2016/03/… Commented Oct 27, 2021 at 17:08
  • Show us what you've tried :) Commented Oct 27, 2021 at 17:08
  • I used this code to get the applicationName: Commented Oct 27, 2021 at 17:25

2 Answers 2

5

I was able to accomplish this by using appsettings.json to define serilog settings and also using code to add a WriteTo method (I needed to do this in code because I am passing the renderMessage parameter to the constructor of JsonFormatter.

Here is what my appsettings.json looks like:

"Serilog": {
"Using": [],
"MinimumLevel": {
    "Default": "Verbose"
},
"Enrich": [ "WithExceptionDetails" ],
"WriteTo": [
    {
    "Name": "Console"
    }
],
"Properties": {
    "Application": "Sample Application"
}
},
"LogsFilePath": "logs/myapp.txt",
"LogsRollingInterval": "Day"

And in my code I did this:

IConfigurationBuilder builder = new ConfigurationBuilder();
builder.AddJsonFile(Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json"));

var config = builder.Build();

Serilog.Log.Logger = new LoggerConfiguration()
    .ReadFrom.Configuration(config)
    .WriteTo.File(new JsonFormatter(renderMessage: true),
        config.GetSection("LogsFilePath").Value,
        rollingInterval: (RollingInterval)Enum.Parse(typeof(RollingInterval), config.GetSection("LogsRollingInterval").Value))
    .CreateLogger();
Sign up to request clarification or add additional context in comments.

Comments

0

Refer below code to configure it using appsettings.json in .net core

return new LoggerConfiguration()
    .WriteTo.File(path: _configuration.GetValue(Constants.SerilogFileConfigurations.Path, "Logs\\Log_.txt")!,
        outputTemplate: _configuration.GetValue(Constants.SerilogFileConfigurations.OutputTemplate, defaultOutputTemplate)!,
        retainedFileCountLimit: _configuration.GetValue(Constants.SerilogFileConfigurations.RetainedFileCountLimit, 5)!,
        restrictedToMinimumLevel: Enum.TryParse(typeof(LogEventLevel), _configuration.GetValue(Constants.SerilogFileConfigurations.LogLevel, "Information"), out object? logLevel)
            ? (LogEventLevel)logLevel! : LogEventLevel.Information,
        rollOnFileSizeLimit: true,
        rollingInterval: RollingInterval.Day)
    .MinimumLevel.Debug();

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.