简体   繁体   中英

Serilog RavenDb sink not working in an asp.net 5 app

I am looking at serilog and running a few tests. So far it is working fine writing to the console or file. However I am not having any luck getting it to work with the RavenDb sink. I am trying to get this working in an asp.net 5 app. I have reviewed the following articles:

http://nblumhardt.com/2015/05/diagnostic-logging-in-dnx-asp-net-5/ http://nblumhardt.com/2013/06/serilog-and-ravendb/

I started with an empty app, and added the following dependencies in project.json.

"Serilog.Framework.Logging": "1.0.0-rc1-final-10071",
"Serilog.Sinks.RavenDB": "1.5.4",
"RavenDB.Client": "3.0.30000"

I also removed dnxcore.

Then I added the following code in startup.cs:

public Startup()
{
    var documentStore = new DocumentStore()
    {
        Url = "http://localhost:8080",
        DefaultDatabase = "Logs"
    }.Initialize();

    Log.Logger = new LoggerConfiguration()
       .WriteTo.File(@"c:\temp\log.txt")
       .WriteTo.Console()
       .WriteTo.RavenDB(documentStore)
       .CreateLogger();
}

public void ConfigureServices(IServiceCollection services)
{
}

public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
    loggerFactory.AddSerilog();
    app.UseIISPlatformHandler();

    app.Run(async (context) =>
    {
        Log.Information("Hello World");
        await context.Response.WriteAsync("Hello World!");
    });
}

Everything gets logged to the file and console just fine, and the Logs database gets created, but no log entries are stored in RavenDb.

I have tried various log levels. I tried reducing the batch size. I suspected this had something to do with the lifecycle of the document store, so I added the following in the ConfigureServices method.

services.AddSingleton(x =>
{
   return new DocumentStore()
   {
       Url = "http://localhost:8080/",
       DefaultDatabase = "Test",
   }.Initialize();
}

Then I moved the logger configuration code into the Configure method and used DI instance, but that doesn't work either. I can store other objects in RavenDb using the same DocumentStore just fine.

Have I missed a configuration setting or something?

I was able to get this working with the latest RavenDb client. I created a new (package) style library, added Serilog nuget package v2.0.0-beta-403, added the RavenDB.Client v3 nuget package, and dropped in the .cs files from the existing Serilog.Sinks.RavenDb library. It compiled and worked, I didn't have to change any code.

I haven't had a chance to test it much yet, but it seem to be working fine. Of course I don't know how stable Serilog v2 beta is, or how long until it is released. The nice thing is that serilog v2 supports .netcore. Unfortunately the RavenDb client doesn't, at least not yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM