DataDog / serilog-sinks-datadog-logs

Serilog Sink that sends log events to Datadog https://www.datadoghq.com/
Apache License 2.0
60 stars 41 forks source link

How to configure exceptionHandler option in appsettings.json? #109

Open jignesh-dalal opened 6 months ago

jignesh-dalal commented 6 months ago

Hi,

I am looking for a way to configure exceptionHandler option in appsettings.json. Is there a way to do so? I think a workaround to achieve this would be to read up appsettings.json in an object and then use the object to manually set up Datadog logging in code as follows:

Serilog.Log.Logger = new LoggerConfiguration().WriteTo.DatadogLogs(configuration["apiKey"], exceptionHandler: ex => { Console.WriteLine(ex); }, otherAppSettingsOptions...)

Please suggest.

Thanks.

gh123man commented 6 months ago

Hi @jignesh-dalal This seems like a limitation to me. I'll open a backlog item for us to expose an API to add the exception handler in these cases.

As you mentioned, reading the config yourself would be a possible workaround. Something like this will work:

var configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile(path: "appsettings.json")
    .Build();

Log.Logger = new LoggerConfiguration()
    .WriteTo.DatadogLogs(configuration["Serilog:WriteTo:1:Args:apiKey"], exceptionHandler: (x) => {
        System.Console.WriteLine(x);
    })
    .CreateLogger();

Note - you will have to extract each key/value pair you care about from the config (such as source/service/tags) and supply them in DatadogLogs. This is not an ideal workaround, but it should accomplish what you are looking for.