Static class with unique id per request

I want to achieve something similar (but much simpler) like Serilog sinks without using DI explicitly.

It's easy to do with DI you just have to register service as scoped and just create id when object is initialized.

But how to achive that with static class? It's somehow possible because .Net Serilog team did that.

If you want to use their logger you should simple inicialize it:

Log.Logger = new LoggerConfiguration()
    .Filter.ByExcluding(c => c.Properties.Any(p => p.Value.ToString().Contains("swagger")))
    .Filter.ByExcluding(c => c.Properties.Any(p => p.Value.ToString().Contains("liveness")))
    .WriteTo.Console(new ElasticLogFormatter())
    .WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(elasticConfiguration.Url))
        AutoRegisterTemplate = true,
        AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv6,
        IndexFormat = elasticConfiguration.Index,
        CustomFormatter = new ElasticLogFormatter()

And whenever you use:


It contains unique request id within single request despite where you use this (controller, service, db context - they all shere the same id).

I've found their code on github but with that it still remains a struggle to find where and how they achived that.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum