trenoncourt / serilog-enrichers-aspnetcore-httpcontext Goto Github PK
View Code? Open in Web Editor NEWEnriches Serilog events with Aspnetcore HttpContext.
License: MIT License
Enriches Serilog events with Aspnetcore HttpContext.
License: MIT License
In serilog.aspnetcore nuget package you can do this:
app.UseSerilogRequestLogging(options =>
{
options.EnrichDiagnosticContext = (diagnosticContext, httpContext) =>
{
diagnosticContext.Set("UserAgent", httpContext.Request.Headers["User-Agent"].ToString());
diagnosticContext.Set("HttpRequestClientHostIP", httpContext.Connection.RemoteIpAddress);
diagnosticContext.Set("HttpRequestUrl", httpContext.Request.GetDisplayUrl());
diagnosticContext.Set("UserName", httpContext.User.Identity.Name == null ? "(anonymous)" : httpContext.User.Identity.Name);
};
});
And the logs for HTTP requests/responses will have that extra information in it.
What does this nuget do in addition?
EDIT: looking into it
I was able to write to the console, but when I modified to write the log in MSSqlServer nothing appeared.
I think it has to do with outputTemplate, right?
Is there any documentation on how to use it to write to MSSqlServer?
Right now the enricher captures the request in the property "body" which is the request body how about adding the response body as well? any thoughts?
context.Response.OnStarting(state =>
{
var httpContext = (HttpContext)state;
//something like the below capture and store the body
// httpContext.Response.Body
return Task.CompletedTask;
}, context);
as this nuget uses HttpContextAccessor to get the http context properties, it cause 404 to next incoming request as the http-method becomes empty string.
please see aspnet/KestrelHttpServer#2591 for more details.
So instead of getting the properties at logging time it would be good idea to set the properties to LogContext in a middle ware so that HttpContextAccessor wont be accessed from a background thread.
This extension is the best I've found so far for logging httpContext in .net core. I couldn't get the others to work correctly.
Is there a way to only include the httpContext at certain log levels?
I'm concerned performance may be an issue if the properties are being added for every event level.
When I moved the LoggerConfiguration to the UseSerilog method I was unable to log the start and end time of the host, in Program.cs, Log in this file no longer work, is there a way to fix this?
The current version doesn't work with .Net Core v3.1. Nothing is parsed in the output.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.