Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

I keep going back and forth on whether this would be better posted in the hangfire or the serilog forums, but figure there are tons of people who use hangfire and not serilog, but I'm looking for people who happen to use both serilog and maybe hangfire.

In short, we use hangfire to run some nightly work and we log during these jobs using serilog - specifically the mssqlserver sink. Most days this works perfect, other days it only logs maybe the very first message (basically "Job Starting"), this is for the exact same job and expected outcome. that it just performed hours earlier.

At first we thought maybe some kind of exception was happening, but we added the basic file log sink and it writes the messages there every time. So it's nice that we have this file log to fall back on, but would really like the mssqlserver sink to work so we can query the results in the app.

I can only assume it has something to do with the batching / flushing. But we're using the static logger (Log.Logger = loggerConfig.CreateLogger();) and everything I've read so far seems to say that using the Host.UseSerilog() extension method is properly configuring any necessary CloseAndFlush(). And there are no actual application errors or anything that would cause it to not reach the default 5 second wait and flush anyway. The applications itself never shuts down during this period and continues to process more work.

Our configuration:
Asp.net core app with .NET 9.
Hangfire 1.8
Serilog 4.2.0
Serilog.Sinks.MSSqlServer 8.2.2

Our program.cs:

var builder = WebApplication.CreateBuilder(args);
...
builder.Host.UseSerilog();
...
var app = builder.Build();
...
app.UseSerilogSinks();

Our MSSqlServer sink config:

    public static WebApplication UseSerilogSinks(this WebApplication app)
    {
        var loggerConfig = new LoggerConfiguration()
            .MinimumLevel.Debug()
            .Enrich.FromLogContext()
            .Enrich.WithProperty("Application", "JobServer");

        loggerConfig = ConfigureDbSink(loggerConfig);

        Log.Logger = loggerConfig.CreateLogger();
        
        return app;
    }

    private static LoggerConfiguration ConfigureDbSink(LoggerConfiguration loggerConfig)
    {
        var msSqlServerSinkOptions = new MSSqlServerSinkOptions
        {
            AutoCreateSqlTable = true,
            SchemaName = "Hangfire",
            TableName = "JobEventLogs"
        };

        var columnOpts = new ColumnOptions();
        columnOpts.Store.Remove(StandardColumn.Properties);
        columnOpts.Store.Add(StandardColumn.LogEvent);
        columnOpts.Id.DataType = SqlDbType.BigInt;
        columnOpts.Id.NonClusteredIndex = true;
        columnOpts.LogEvent.DataLength = -1;
        columnOpts.TimeStamp.NonClusteredIndex = true;
        columnOpts.AdditionalColumns = new Collection<SqlColumn>
        {
            new SqlColumn { ColumnName = "JobId", DataType = SqlDbType.BigInt, NonClusteredIndex = true, AllowNull = false },
            new SqlColumn { ColumnName = "RecurringJobId", DataType = SqlDbType.NVarChar, NonClusteredIndex = true, AllowNull = true, DataLength = 512 },
            new SqlColumn { ColumnName = "SourceContext", DataType = SqlDbType.NVarChar, NonClusteredIndex = true, AllowNull = true, DataLength = 256 }
        };

        loggerConfig = loggerConfig.WriteTo.Logger(lc => lc
            .Filter.ByIncludingOnly(Matching.WithProperty("JobId"))
            .WriteTo.MSSqlServer(..., msSqlServerSinkOptions, columnOptions: columnOpts)
        );

        return loggerConfig;
    }

Anyone have any ideas or places to start?

You must be logged in to vote

Replies: 1 comment

Comment options

Hi @johnhwright

Thank you for getting in touch.

There ia a batched and a non batched (audit) version of the MSSQL sink. To confirm your suspicion, that the problem you see is caused by the batching, could you please test again after replacing the .WriteTo with .AuditTo in your startup code?

Cheers,
Christian

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.