3

I added log file in my .netcore MVC project by using serilog and I read all parameter appsettings.json file. Serilog configuration is (in appsettings) like this.

In This solution, all logs are written in the one file. But I want to write different error message different files. Such as my X method errors are written xMethodLogs.txt and Y method errors are written yMethodLogs.txt

MyStartup.cs file is

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();
        loggerFactory.AddSerilog();

        app.UseMvc();
        app.UseMvc(routes =>
        {
            routes.MapRoute("default", "api/{controller=Home}/{action=Index}/{id?}/{sad?}");
        });
    }


public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
        Configuration = builder.Build();
    }

My appsettings file is

"Serilog": { 
    "Using": [ "Serilog.Sinks.RollingFile" ], 
    "MinimumLevel": "Error", 
    "WriteTo": [ { 
        "Name": "RollingFile", 
        "Args": { "pathFormat": "C:\\MyPro\\ServiceLog.txt" } 
    } ], 
    "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ], 
    "Properties": { "Application": "Sample" } 
}
Béranger
  • 671
  • 8
  • 23
dincer.unal
  • 67
  • 1
  • 2
  • 13
  • My appsettings file is ` "Serilog": { "Using": [ "Serilog.Sinks.RollingFile" ], "MinimumLevel": "Error", "WriteTo": [ { "Name": "RollingFile", "Args": { "pathFormat": "C:\\MyPro\\ServiceLog.txt" } } ], "Enrich": [ "FromLogContext", "WithMachineName", "WithThreadId" ], "Properties": { "Application": "Sample" } }` – dincer.unal Jul 24 '17 at 07:12
  • You can take a look at this thread https://stackoverflow.com/questions/28292601/serilog-multiple-log-files `Filter.ByIncludingOnly` – Béranger Jul 24 '17 at 07:22
  • I research this url but Filter.ByIncludingOnly is not working .netcore and I dont seperate logs error or warning. My logs are error in different methods or classes. I want to seperate different classess errors or differents methods errors – dincer.unal Jul 24 '17 at 07:34
  • I don't really know serilog but I did not find a way to create multiple loggers or something like that. Could you consider about changing the library ? – Béranger Jul 24 '17 at 07:37
  • I dont know If I dont find any solition using serilog, I will change. Did you refer any library in this solition? – dincer.unal Jul 24 '17 at 07:49
  • You can achieve what you want with NLog https://github.com/nlog/nlog/wiki – Béranger Jul 24 '17 at 07:57
  • 1
    See this answer for the solution...https://stackoverflow.com/questions/28292601/serilog-multiple-log-files?noredirect=1&lq=1 – dinotom Nov 28 '17 at 02:10
  • Thaks @dinotom I solve this problem using NLog but I will note this solution ((: – dincer.unal Nov 28 '17 at 08:03

0 Answers0