Search code examples
azure-cosmosdbserilogazure-cosmosdb-sqlapi

Using Xml-configuration to log to Cosmos DB with serilog


I am trying to go away from statically linking my code to specific serilog sinks and use xml-configuration in appsettings instead.

I used to have code like this:

serilogConfig = serilogConfig.WriteTo.AzureDocumentDB(
                    new Uri(logConfig.AzureDocumentDBConfig.Endpoint),
                    logConfig.AzureDocumentDBConfig.AuthKey,
                    logConfig.AzureDocumentDBConfig.Database,
                    logConfig.AzureDocumentDBConfig.Collection);

The C#-code to load from appsettings is this:

var log = new LoggerConfiguration()
                .ReadFrom.AppSettings()
                .CreateLogger();

I am trying to change this to e.g. this configuration data

<add key="serilog:using:AzureDocumentDB" value="Serilog.Sinks.AzureDocumentDB" />
<add key="serilog:write-to:AzureDocumentDB.endpointUrl" value="https://my_prefix.documents.azure.com:443/" />
<add key="serilog:write-to:AzureDocumentDB.authorizationKey" value="mykey" />
<add key="serilog:write-to:AzureDocumentDB.databaseName" value="mydatabasename" />
<add key="serilog:write-to:AzureDocumentDB.collectionName" value="mycollectionname" />
<add key="serilog:write-to:AzureDocumentDB.timeToLive" value="3600" />

My problem: nothing is written to Cosmos.

Am I missing something obvious here? I have checked the config values a million times - and they are correct. I have also tried to use non-existing values for "databaseName" and "collectionName", and serilog creates these artifacts just fine in DocumentDB - to it is able to connect to Cosmos.

And finally - this is the entire code in my test console app:

var log = new LoggerConfiguration()
                .ReadFrom.AppSettings()
                .CreateLogger();

log.Information("Test");
log.Error(new ApplicationException("some error"), "stuff failed");
log.Warning("Test2");

Console.WriteLine("Press Enter to exit...");

Logging level in app config is this:

<add key="serilog:minimum-level" value="Verbose" />

Thanks :-)

UPDATE:

I have created a completely new Visual Studio project where I am using the fluent configuration API. My code is this:

var logger = new LoggerConfiguration()
    .WriteTo.ColoredConsole()
    .WriteTo.AzureDocumentDB("https://myendpoint.documents.azure.com:443/", "mykey")
    .CreateLogger();

The behavior is exactly the same. Nothing is written to Cosmos. If I do not specify collection name and database name, a "Logs" collection and a "Diagnostics" database is created in Cosmos - but nothing is written.

I am beginning to question life itself and the meaning with it ... what am I missing here?

For testing purposes I have created a new Cosmos instance and downloaded the "ToDo"-app that writes to the database. This one works, and if I change the pointers to my original Cosmos DB, it can write to this as well. My own code cannot write to the new DB created for test.


Solution

  • Well, it turns out that I was indeed missing something obvious. The application thread ended before data could be pushed/flushed to Cosmos.

    https://github.com/serilog/serilog-sinks-azuredocumentdb/issues/82