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" }
}