I am trying to Log errors and information for each service using .net core. How can i use separate files for logging.
Currently i am using one file for log all errors and information.
Here is my Program.cs and Startup.cs
// Program.cs
public static void Main(string[] args)
{
CreateWebHostBuilder(args)
.ConfigureLogging((hostingContext, logging) =>
{
// Requires `using Microsoft.Extensions.Logging;`
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
logging.AddDebug();
logging.AddEventSourceLogger();
})
.Build()
.Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureLogging(logging =>
{
logging.ClearProviders();
logging.AddConsole();
});
// Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddFile("Logs/SmsDomeLogs-{Date}.txt");
//.........
}
An idea as a workaround could be to create a custom logger which writes the data to file, and you can create the file name to contain some kind of custom string from your services. I'm using this way, too. For ex.:
And of course, you can improve how you want, add log levels, etc.