-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create multiple log files using Serilog #72
Comments
Hi @nblumhardt any help with this. |
@nblumhardt i have created an application with logging functionality using Serilog. I would like to create two log files. One log file for application startup and the other file for other functionality of the application. |
Hi @ranjithjains , take a look at Serilog.Sinks.Map , the README more or less covers that scenario. |
It's also possible just to create two separate |
any solution yet? |
@Maru9961 there are 2 solutions, both already mentioned here in this thread.
Log.Logger = new LoggerConfiguration()
.WriteTo.Logger(x =>
{
x.WriteTo.File("event_type_1.txt");
x.Filter.ByIncludingOnly(e => e.Properties["event_type"] == "type_1");
})
.WriteTo.Logger(x =>
{
x.WriteTo.File("event_type_1.txt");
x.Filter.ByIncludingOnly(e => e.Properties["event_type"] == "type_2");
})
.CreateLogger();
Log.Logger = new LoggerConfiguration()
.WriteTo.Map("event_type", "type_1", (name, wt) => wt.File("event_type_1.txt"))
.WriteTo.Map("event_type", "type_2", (name, wt) => wt.File("event_type_2.txt"))
.CreateLogger(); |
@cocowalla Thank you for your code above! |
@frankmehlhop I think you are asking how to log as As per the docs, there are multiple ways to do this:
var eventType1Logger = Log.ForContext("event_type", "type_1");
using (LogContext.PushProperty("event_type", "type_1"))
{
// Process request; all logged events will carry `event_type`
Log.Information("This log message will have an event_type property");
} You can do this for individual logs, or with an extensibility point in whatever framework you are using. For example, if you are using ASP.NET Core, you could use middleware: public async Task Invoke(HttpContext context)
{
if (!some_check_to_add_event_type_1)
return;
using (LogContext.PushProperty("event_type", "type_1"))
{
await this.next.Invoke(context);
}
}
Log.Logger = new LoggerConfiguration()
.Enrich.WithProperty("event_type ", "type_1")
.Enrich.WithProperty("Environment", ConfigurationManager.AppSettings["Environment"])
// Other logger configuration
For (3), if you want to attach the public static class LogExtensions
{
public static ILogger AsEventType1(this ILogger log)
=> log.ForContext("event_type", "type_1");
}
...
Log.AsEventType1().Error("Something happened!"); |
Thank you! |
I would like to know if we can log to multiple files in Serilog.
For example, i would like to create logs of TYPE1 to one file and TYPE2 to another file.
The text was updated successfully, but these errors were encountered: