Skip to content
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

Closed
ranjithjains opened this issue Oct 18, 2018 · 9 comments
Closed

Create multiple log files using Serilog #72

ranjithjains opened this issue Oct 18, 2018 · 9 comments

Comments

@ranjithjains
Copy link

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.

@ranjithjains
Copy link
Author

Hi @nblumhardt any help with this.

@ranjithjains
Copy link
Author

@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.

@tsimbalar
Copy link
Member

Hi @ranjithjains ,

take a look at Serilog.Sinks.Map , the README more or less covers that scenario.

@nblumhardt
Copy link
Member

It's also possible just to create two separate Loggers via separate LoggerConfigurations and use them independently. Closing as this is a stale ticket now, but please let us know if you need more details @ranjithjains .

@Maru9961
Copy link

any solution yet?

@cocowalla
Copy link

@Maru9961 there are 2 solutions, both already mentioned here in this thread.

  1. Create a sink for each type:
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();
  1. Use Serilog.Sinks.Map, which does the same but with nicer config syntax:
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();

@frankmehlhop
Copy link

frankmehlhop commented Jan 27, 2021

@cocowalla Thank you for your code above!
I wonder how can I use the two different types (1 and 2) in code also on the run?
I think of some thing like "__logger.Log<type_2>("...");"?

@cocowalla
Copy link

@frankmehlhop I think you are asking how to log as event_type 1 and 2 at runtime? This is done using logging context.

As per the docs, there are multiple ways to do this:

  1. Create a logger just for such events:
var eventType1Logger = Log.ForContext("event_type", "type_1");
  1. Use an ambient/temporary log context
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);
    }
}
  1. You can use globally attached properties:
Log.Logger = new LoggerConfiguration()
    .Enrich.WithProperty("event_type ", "type_1")
    .Enrich.WithProperty("Environment", ConfigurationManager.AppSettings["Environment"])
    // Other logger configuration
  1. You can use an ILogEventEnricher to programmatically enrich logs

For (3), if you want to attach the event_type on a log-by-log basis, you can make it nicer with an extension method:

public static class LogExtensions
{
    public static ILogger AsEventType1(this ILogger log)
        => log.ForContext("event_type", "type_1");
}
...
Log.AsEventType1().Error("Something happened!");

@frankmehlhop
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants