Skip to content

ravendb/serilog-sinks-ravendb

Repository files navigation

Serilog.Sinks.RavenDB

Build status NuGet Version

A Serilog sink that writes events as documents to RavenDB.

Package - Serilog.Sinks.RavenDB | Platforms - .NET Standard 2.0 (.NET 4.6.1 or later; .NET Core 2.0 or later)

var logs = new DocumentStore { ConnectionStringName = "Logs" }.Initialize();

var log = new LoggerConfiguration()
    .WriteTo.RavenDB(logs)
    .CreateLogger();

You'll need to create a database on the server for logs, and specify this as your default database in the connection string or DocumentStore.DefaultDatabase. In the alternative, you can pass a default database when configuring the RavenDB sink. More information.

You can also configure the sink through your application config file using Serilog.Settings.AppSettings

Log.Logger = new LoggerConfiguration()
    .ReadFrom.AppSettings()
    .CreateLogger();
<connectionStrings>
  <add name="Logs" connectionString="Url=http://[RAVEN_DB_SERVER]:8080/;DefaultDatabase=[OPTIONAL_DEFAULT_DATABASE]" />
</connectionStrings>
<appSettings>
  <add key="serilog:minimum-level" value="Information" />
  <add key="serilog:using:RavenDB" value="Serilog.Sinks.RavenDB" />
  <add key="serilog:write-to:RavenDB.connectionStringName" value="Logs" />
  <add key="serilog:write-to:RavenDB.defaultDatabase" value="[DEFAULT_DATABASE]" />
</appSettings>

Automatic Log Record Expiration

If you install the RavenDB expiration bundle on the database where log records are stored, you can configure the sink to automatically delete log records. There are two ways to do this:

  • Simple version: passing errorExpiration (for fatal and error messages) and expiration (for all other messages). If you pass one, you should pass both. Timeout.InfiniteTimeSpan indicates that messages of the appropriate type will never be deleted by the expiration bundle.
  • Featured version: passing logExpirationCallback, a which will receive a Serilog LogEvent and return a TimeSpan. Timeout.InfiniteTimeSpan indicates that the message will never be deleted by the expiration bundle.