Thanks for trying @mistyn8, I’m quite happy with the way that exceptions are logged, my problem was that I needed to use my own custom enricher(s).
As far as I understand. Umbraco calls .Enrich.FromLogContext()
during Serilog setup as a way to allow for adding custom enrichers. The problem is that these are scoped to the current LogContext, and when an unhandled exception happens, this scope is disposed. This means that any “per-request”-registered enrichers will not run. E.g. the built-in ones in Umbraco like SessionIdEnricher, RequestNumberEnricher, and RequestIdEnricher will not run for unhandled exceptions.
The only way (that I’ve found) to get around this without updating the source of Umbraco is to use appsettings.json
to register the enricher in the Serilog-section.
"Using": [ "MyProject.Web" ],
"Enrich": [
{ "Name": "WithMyCustomEnrichers" }
]
Then have an extension method in MyProject.Web
namespace like so:
public static LoggerEnrichmentConfiguration WithMyCustomEnrichers(
this LoggerEnrichmentConfiguration enrichmentConfiguration)
{
if (enrichmentConfiguration == null)
throw new ArgumentNullException(nameof(enrichmentConfiguration));
// Notice: No idea trying to use StaticServiceProvider.Instance as it will be null.
// because this method is called before the service provider is built.
enrichmentConfiguration.With<RequestUserAgentEnricher>();
return enrichmentConfiguration;
}
The enrichers need to have a parameterless constructor. If you need access to DI you’d have to use StaticServiceProvider.Instance
to get access to “stuff” from DI. A good option is to use Lazy properties for this.