Serilog and ASP.NET Core enrich `WithUserName` and `WithHttpRequestUserAgent`? - asp.net-core

Is there a way to enrich Serilog log data in ASP.NET Core 2.1 application, similar to how enrichers WithUserNameand WithHttpRequestUserAgent work for classic ASP.NET (System.Web)?
I tried implementing ILogEventEnricher interface, but it does not work, because I am not able to gain access to RequestContext from my custom enricher.

As pointed out in the comments it seems like Add Username into Serilog would serve your purpose and would also be the duplicate of this issue.

Related

Is there an alternative to batching requests (MapHttpBatchRoute) in ASP.NET Core WebAPI?

I'm working on migrating an ASP.NET WebAPI application to ASP.NET Core. In this application I'm using batch routes (as described here: https://devblogs.microsoft.com/aspnet/introducing-batch-support-in-web-api-and-web-api-odata/). It's quite elegant - all I need to do is to map a batch route using config.Routes.MapHttpBatchRoute, and then just batch whatever API requests I want to from the client. However, I haven't found an alternative to this in ASP.NET Core. Does it exist, or do I need to write batch APIs myself that perform what I need to do?
I've had success using this library: https://github.com/Tornhoof/HttpBatchHandler
It works for .Net Core 3.0 and newer. The documentation isn't the best though.

AspNet Core AppInsights Integration

I am integrating app insights into our AspNet Core app(Target Framework .Net 4.7.1). I have two queries regarding app insights integration.
I am using SimpleInjector IOC, so does it make sense to have below line of code to inject AI into Asp Net Core DI?
services.AddApplicationInsightsTelemetry
I'm having my own Logger class which initializes TelemetryCLient and Logger class is injected using SimpleInjector. So removing above line code will cause an issue or lack of feature from ASPNet Core perspective?
In Asp.Net when we use to add AI it uses to add ApplicationInsights.config file which contains TelemetryInitializer's and TelemetryModules. Whats the best parctice in AspNet Core 2.1 for this? How do I add following TelemetryInitializers?
HttpDependenciesParsingTelemetryInitializer
AzureRoleEnvironmentTelemetryInitializer
AzureWebAppRoleEnvironmentTelemetryInitializer
OperationCorrelationTelemetryInitializer
etc...
Thanks in advance!!!
services.AddApplicationInsightsTelemetry is the easiest way to add application insights to your project. It sets up auto-collection modules for Requests, Dependencies etc, sets up default TelemetryInitializers, TelemetryProcessors (for sampling, live metrics etc.)
if you don't use services.AddApplicationInsightsTelemetry, then you have to programmatically setup all modules/initializers/sampling etc yourself.
There is no ApplicationInsights.config file, so pretty much every customization of the config is to be done through code. Following shows how to add/remove telemetry initializers.
https://github.com/Microsoft/ApplicationInsights-aspnetcore/wiki/Custom-Configuration#configure-telemetry-initializers

WebTelemetryInitializerBase in ASP.NET Core / MVC6

Is there an MVC6 compatible version of WebTelemetryInitializerBase that would work with ASP.NET Core (on the full .NET Framework)?
See my question here where I asked how to get HttpContext in my temeletry initializers. Unfortunately I didn't specify that I was using MVC 6 and thus no System.Web.HttpContext.
Yes, there is a version of this for aspnetcore. Check out the Microsoft Application Insights for ASP.NET Core applications repo.
There is an implementation of getting the WebUser found in /src/Microsoft.ApplicationInsights.AspNetCore/TelemetryInitializers/WebUserTelemetryInitializer.cs which you can use as a guide.
The TelemetryInitializerBase class is the one that consumes the IHttpContextAccessor which is used to get the HttpContext.
From there you can get the Microsoft.AspNetCore.Http.HttpContext.User which is they type of System.Security.Claims.ClaimsPrincipal

Migrating from OWIN to ASP.NET Core

When moving from OWIN to ASP.NET Core, I've found a bit of information about dependencies to migration, but I've not found information about these other topics:
The middle-ware pipeline. How is this different, or not?
The DelegatingHandler pipeline (e.g. Web API). How is this different, or not?
The startup.cs file. How is this different?
In short, it would be great to know what are the primary hot-spots that would need my attention, in order to move from OWIN to ASP.NET Core.
As a first example - ASP.NET Core does not appear to have HttpConfiguration. There are myriads of example plugins and services that assume its existence. I'd like to infer how to translate instructions for HttpConfiguration into whatever approach ASP.NET Core expects.
As another example, the Swashbuckle documentation gives instructions for setup with OWIN, but those instructions don't work with ASP.NET Core. If I understood the primary differences from OWIN, it would be easier to "guesstimate" how to install Swashbuckle into ASP.NET Core.
Note: Swashbuckle also gives instructions for self-hosted apps. That confuses me because I think of OWIN (vis-a-vis Katana) as being self-hosted already, and so it sounds redundant to me. But I don't think this is related to the present question.
My question has used Swashbuckle as an example, but I am not asking about Swashbuckle specifically.
Update
I've discovered that much of the information I'm looking for is in the article Transitioning from Web API 2 to ASP.NET MVC 6.
Middleware is quite similar between Katana and Core but you use HttpContext instead of IOwinContext.
Startup.cs is similar but there's much more DI support.
WebApi has been merged into MVC
DelegatingHandler is gone, use middleware instead.
HttpConfiguration has been split up into Routing and MvcOptions.
Also https://devblogs.microsoft.com/aspnet/katana-asp-net-5-and-bridging-the-gap/
I think you can start here. It's an entire chapter about OWIN with ASP.NET Core. Hope this helps.

Difference between Elmah and Elmah.Contrib.WebApi

I am confused between Elmah and Elmah.Contrib.WebApi. And which one is best option for Web API. Iam already using Nlog for exception logging along with Tracing in Web API. So How Elmah is different from all of them. What is the exact need to going for Elmah??
Thanks in Advance
I find that it is useful for catching errors that you did not catch and log yourself. I am using it for a MVC application and it sends me an email when I have an issue that I need to resolve. I can be proactive and I am working on errors before I can hear from the user.
Elmah addresses your default application error logging. However when you're using Web API, for example ASP.NET MVC Web API, you need some extra logic to log your Web API errors within your Elmah data store.
There are a few ways to address this requirement, one being to use the Elmah.Contrib.WebApi package.
Once the package is imported to your project remember to follow any implementation requires, i.e. startup filter registry (as noted here)