0

I have this code written:

 return Host.CreateDefaultBuilder()
                .UseSerilog((context, services, configuration) => configuration
                    .MinimumLevel.Verbose()
                    .ReadFrom.Configuration(_configuration)
                    .ReadFrom.Services(services)
                    .Enrich.FromLogContext()
                    .Enrich.With(UserLoginEnricher.GetInstance()) 
                    .Enrich.With(new LoggersAdditionalEnricher())
 //CSV logger
                    .WriteTo.Logger(x =>
                    {
                        x.WriteTo.File(ApplicationConfiguration.FullPathToCsvFile,
                            rollingInterval: RollingInterval.Day, 
                            encoding: Encoding.Unicode, 
                            retainedFileCountLimit: ApplicationConfiguration.AppSettingsOptions.RetainDataCount, 
                            outputTemplate: "{Message}{NewLine}");

                        //log filter
                        x.Filter.ByIncludingOnly(e =>
                        {
                            if (!e.Properties.ContainsKey(nameof(LogType)))
                                return false;
                            return e.Properties[nameof(LogType)].ToString() == LogType.CSV.ToString();
                        });
                    })
                    .WriteTo(x =>{...})
                    .WriteTo(x =>{...});

Is there any way to configure json SERILOG configuration to change minimum level of File logger without code changes?

My JSON config of serilog:

"Serilog": {
    "Using": [
      "Serilog.Sinks.File"
    ],
    "MinimumLevel": {
      "Default": "Verbose",
      "Override": {
        "Microsoft": "Warning",
        "Microsoft.EntityFrameworkCore.Database.Command": "Warning"
        }
    }
  }

I have more loggers. I need all loggers to write with Warning level and this CSV logger to write on Information level


UPD:

The problem: I have many 'WriteTo's which write to files. They all are written in code. I need to make one logger to write to level 1, and others to another level WITHOUT code changes.

I guess i cannot rewrite these loggers with json configuration, because these loggers generate file name from machine name, current windows username, logfile type and date:

(ex. [RU-PF10PUV1][Vlad.Pupkin][CalendarCollector_OutlookCalendarLog][_20220921.csv)

Is this right?

Also i need to know is it possible to somehow override existing code configuration with json config. Something like

 "Override": 
{
"Utility.Logging.LogType.CSV": "Information"// LogType.CSV is a property of logged message
}

Is there any way to solve this problem

1 Answers1

0

You can use this nuget package for doing configuration in appsettings.json for Serilog. https://github.com/serilog/serilog-settings-configuration

and there great extension which even helps us to convert code to proper configuration. https://marketplace.visualstudio.com/items?itemName=Suchiman.SerilogAnalyzer

and to restrict log level for a particular sink you can specify like this

"WriteTo": [
      {
        "Name": "File",
        "Args": { "restrictedToMinimumLevel": "Information" }
      }
    ]

you need to add other arguments as per your requirement.

Hope it helps.

CodingMytra
  • 2,234
  • 1
  • 9
  • 21
  • I already have this nuget: i mentioned json config for serilog. Sadly, i cannot use this config to rewrite coded serilog WriteTos, because i need to generate file names. I need a way to somehow override particular coded 'WriteTo' configuration with json configuration. – Владимир Sep 23 '22 at 08:01