Ramakrishna Reddy
Ramakrishna Reddy

Reputation: 347

not getting any error when serilog is not able to insert data in sql server

I am using serilog as logging framework in .net core 2.0 project and i am trying to store the logs in sql server but serilog is not storing any data in database and it is not even returning error.

can any one help how to resolve this issue and is it possible to add file approach to store logs when database fails to store

Upvotes: 2

Views: 1648

Answers (1)

bri
bri

Reputation: 3030

Serilog.Debugging.SelfLog

You can use the SelfLog property to tell serilog where to log it's own errors (all of us have had to debug the logger at some point).

Sample Code

Because I hate providing an answer without sample code that others might find useful ... here is the code we use to "initialize" our logger (including serilog and seq -- a great combo for generating centralized logs that the devops team can monitor).

Serilog.Debugging.SelfLog.Enable(Console.Error);
ILoggerFactory factory = new LoggerFactory();
factory.AddConsole();
factory.AddDebug();
var env = "PROD"; //MyEnvironment: PROD, STAGE, DEV, ETC

var seqLogger = new LoggerConfiguration()
    .MinimumLevel.Information()
    .Enrich.FromLogContext()
    .Enrich.WithProperty("Environment", env)
    .WriteTo.Seq(
        "logserveraddress",
        Serilog.Events.LogEventLevel.Verbose,
        1000,
        null,
        "LogServerApiKey")
    );
    if (env.ToLower() == "prod") { seqLogger.MinimumLevel.Warning(); }
    factory.AddSerilog(seqLogger.CreateLogger());
}
return factory.CreateLogger("NameThisLogInstaceSomethingUseful");

Upvotes: 2

Related Questions