0

I want to separate my logs in three different parts. Searched for logging libraries and the best one I found is Serilog.

  1. write http request logs to daily rolling txt files.
  2. write logs containing exceptions to Seq.
  3. write all logs except http request logs to a local SQLite database (for users access).

Used the following code but nothing works:

new LoggerConfiguration().Enrich.WithExceptionDetails(new DestructuringOptionsBuilder()
.WithDefaultDestructurers()
.WithRootName("Message").WithRootName("Exception").WithRootName("Exception"))
.Enrich.WithProperty("App.Version", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "0.0.0.0")
.Enrich.WithMachineName()
.Enrich.WithEnvironmentUserName()
            .WriteTo.SQLite(GetFilePath("logs.db"))
            .Filter.ByIncludingOnly(Matching.FromSource("Serilog.AspNetCore.RequestLoggingMiddleware"))
            .WriteTo.File(
                GetFilePath("access.log"),
                rollingInterval: RollingInterval.Day,
                retainedFileCountLimit: 31
            )
            .Filter.ByIncludingOnly(LogEvent => LogEvent.Exception != null)
            .WriteTo.Seq("https://mylogger.appserver.com/")

Removed second filter (request logs) and now it just writes exception logs in both Seq and local SQLite db.

1
  • did you chop the tail off this message, or is it missing a CreateLogger and/or assigning ti to Log.Logger? When you say nothing works, it's worth checking things like that Commented Jun 22, 2024 at 8:37

1 Answer 1

1

You could use "sub logger" to configure different filter for each sink like following:

Log.Logger = new LoggerConfiguration()
                .Enrich.WithExceptionDetails(new DestructuringOptionsBuilder()
                .WithDefaultDestructurers()
                .WithRootName("Message").WithRootName("Exception").WithRootName("Exception"))
                .Enrich.WithProperty("App.Version", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "0.0.0.0")
                .Enrich.WithMachineName()
                .Enrich.WithEnvironmentUserName()
                .WriteTo.Logger(l => l
                    .Filter.ByExcluding(Matching.FromSource("Serilog.AspNetCore.RequestLoggingMiddleware"))
                    .WriteTo.SQLite(GetFilePath("logs.db")))
                .WriteTo.Logger(l => l
                    .Filter.ByIncludingOnly(Matching.FromSource("Serilog.AspNetCore.RequestLoggingMiddleware"))
                    .WriteTo.File(GetFilePath("access.log"),rollingInterval: RollingInterval.Day,retainedFileCountLimit: 31))
                .WriteTo.Logger(l => l
                    .Filter.ByIncludingOnly(LogEvent => LogEvent.Exception != null)
                    .WriteTo.Seq("https://mylogger.appserver.com/"))
                .CreateLogger();

(Use Filter.ByExcluding to exclude a source)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.