Sentry.NLog and ${aspnet-user-identity} as user of event
See original GitHub issuePlease mark the type framework used:
- ASP.NET MVC
- ASP.NET Web API (OWIN)
- ASP.NET Core
- WPF
- WinForms
- Xamarin
- Other:
Please mark the type of the runtime used:
- .NET Framework
- Mono
- .NET Core
- Version:
Please mark the NuGet packages used:
- Sentry
- Sentry.Serilog
- Sentry.NLog
- Sentry.Log4Net
- Sentry.Extensions.Logging
- Sentry.AspNetCore
- Version: 1.2.0
The TLDR version is basically; I want to set the username in the Sentry NLog events to ${aspnet-user-identity}, any idea how this can be done?
The longer version:
SDK is initialized in Global.asax.cs, and on unhandled exceptions we’re setting a scope to include the currently authenticated user (logged in via Azure AD). This works as expected for unhandled exceptions. (I’ve tried using sendDefaultPii
here, but that only included the service user for the application, which isn’t of interest to us.)
We also have a Sentry NLog target configured for log events containing exceptions where we don’t necessarily want to throw an exception, but we want to send it to Sentry. Some of these exceptions are happening within the scope/request of an authenticated user, but for some reason this information is not included in the Sentry event.
I’ve tried setting sendDefaultPii
in the NLog configuration, with no luck. I’ve also tried sending a custom tag with the user/username explicitly set:
<tag name="user" layout="username:${aspnet-user-identity}" />
However, this information is not included in the event being sent, so it’s either ignored or filtered out. I’ve tried adding ${aspnet-user-identity} to log layout and the currently logged in user is being shown as expected there. Is there a way to set the scope of a log event with a specific username, similar to how we’re doing it in Global.asax.cs?
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (7 by maintainers)
Top GitHub Comments
Yes, we’ve upgraded Sentry packages to v2.0.1 and with some additional log target configuration:
<user username="${aspnet-user-identity}" ipAddress="${aspnet-request-ip}" />
, the username (and IP address in this case) is now included as expected. Thanks! 😃So you’re saying that the
aspnet-user-identity
variable works when you use it as the log message layout, but it gets dropped if it’s included as a tag? That certainly is strange. I assume you’re already referencingNLog.Web
and including it in the NLog config file?If you could post a minimal sample of your nlog config and some of the code of setting up the scope that could help nail down the problem.
Have you tried setting the username etc in code e.g.:
? If none of that proves helpful, there are a few other ways you can set the variable for NLog to use. You could use one of NLog’s diagnostic context classes (e.g.
ndlc
) or variables, although both are much less ideal than having it just work, they could work as a temporary solution.