Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for enricher constructor dependency injection #18

Open
osudude opened this issue Aug 15, 2017 · 7 comments
Open

Add support for enricher constructor dependency injection #18

osudude opened this issue Aug 15, 2017 · 7 comments

Comments

@osudude
Copy link
Contributor

osudude commented Aug 15, 2017

In .NET core, Microsoft got rid of good old HttpContext.Current. You now have to get at it by injecting IHttpContextAccessor into your class constructor that needs to read various http header values. It would be nice if we could inject IHttpContextAccessor into our constructor for enriching values based upon the current http request context (yes, this is possible via middleware but P.I.T.A.). Enricher properties of interest would be various request headers and the user name for the current request.

@nblumhardt
Copy link
Member

Thanks for the note. Perhaps this would fit under the https://github.com/serilog-web projects, somewhere?

So far, for .NET Core, middleware has been working pretty well, though - e.g. https://blog.getseq.net/smart-logging-middleware-for-asp-net-core/

@fish0185
Copy link

fish0185 commented Jun 22, 2018

how about inject IServiceCollection

public class RequestIdEnricher : ILogEventEnricher
    {
        private readonly IHttpContextAccessor _httpContextAccessor;

        public RequestIdEnricher(IServiceCollection services)
        {
            var sp = services.BuildServiceProvider();
            _httpContextAccessor = sp.GetService<IHttpContextAccessor>();
        }

        public void Enrich(LogEvent logEvent, ILogEventPropertyFactory propertyFactory)
        {
            if (logEvent == null)
            {
                throw new ArgumentNullException("logEvent");
            }

            string correlationId = _httpContextAccessor?.HttpContext?.Request?.Headers["CorrelationId"];

            if (string.IsNullOrWhiteSpace(correlationId))
            {
                return;
            }

            var correlationIdProperty = new LogEventProperty("CorrelationId", new ScalarValue(correlationId));
            logEvent.AddPropertyIfAbsent(correlationIdProperty);
        }
    }

@NibblyPig
Copy link

Enrich.With<T> requires T to have a parameterless constructor, so you can't DI anything into it.

@nblumhardt
Copy link
Member

@nblumhardt Enrich.With(new T(...)) is available, too, so you can pass parameters to the constructor of T using that method.

@nblumhardt
Copy link
Member

Folks watching this thread, check out the IDiagnosticContext example in Request Logging over at the ASP.NET Core integration lib; not exactly what's being sought after here, but via middleware does do a good job of covering many cases. HTH!

@spottedmahn
Copy link

example in Request Logging over

updated link: Request Logging

@nblumhardt
Copy link
Member

You can now ReadFrom.Services() and pass an IServiceCollection from which sinks/enrichers/filters may be resolved. Might not cover all cases here but has to be pretty close. HTH!

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

No branches or pull requests

5 participants