Skip to content
This repository has been archived by the owner on Apr 18, 2022. It is now read-only.

Cannot write to Cosmos DB #82

Closed
jrossstocholm opened this issue Mar 28, 2019 · 2 comments
Closed

Cannot write to Cosmos DB #82

jrossstocholm opened this issue Mar 28, 2019 · 2 comments

Comments

@jrossstocholm
Copy link

I am having problems writing documents to Cosmos DB.

I have made two attempts to write to Cosmos - using the appsettings configuration API and using the fluent configuration API.

The sink can connect to the Cosmos DB in question and also create a "Diagnostics" database and a "Logs" collection if those values are not specified. But nothing gets logged to Cosmos DB.

I have downloaded the "ToDo"-sample app from Azure to verify that documents can indeed be written to my Cosmos - and that works. But not the AzureDocumentDB sink.

My fluent configuration is this:

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

logger.Information("Hello friend");

I have updated all dependent nuget packages in the Visual Studio project.

My appsettings configuration is this:

<add key="serilog:using:AzureDocumentDB" value="Serilog.Sinks.AzureDocumentDB" />
<add key="serilog:write-to:AzureDocumentDB.endpointUrl" value="https://myendpoint.documents.azure.com:443/" />
<add key="serilog:write-to:AzureDocumentDB.authorizationKey" value="mykey" />
<!-- Liefspan of log messages in DocumentDB in seconds, leave empty to disable expiration. -->
<add key="serilog:write-to:AzureDocumentDB.timeToLive" value="" />

With C# initialization:

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

A long shot to the root cause of the issue could be the addition of CORS to Cosmos a couple of months back, but it is just a guess.

More details are available on SO at https://stackoverflow.com/questions/55377272/using-xml-configuration-to-log-to-cosmos-db-with-serilog where I originally logged the issue.

@saleem-mirza
Copy link
Contributor

saleem-mirza commented Mar 28, 2019

I believe test code is hosted in console application and it is terminating before logger gets chance to send log to CosmosDB.
For short lived application or process it is recommended to dispose logger manually to make sure log is transmitted.

I have modified code slightly and can see log in destination store.

        using (var logger = new LoggerConfiguration()
           .WriteTo.ColoredConsole()
           .WriteTo.AzureDocumentDB("https://myendpoint.documents.azure.com:443/", "mykey")
           .CreateLogger()) 
        {
            logger.Information("Lorem ipsum dolor sit amet, consectetur adipiscing elit");
        }

@jrossstocholm
Copy link
Author

Thank you, @saleem-mirza - I was just about to loose my mind. I made your edits to my code as well, and it works now.

Thanks for the speedy response!

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

No branches or pull requests

2 participants