question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sentry.NLog and ${aspnet-user-identity} as user of event

See original GitHub issue

Please 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:closed
  • Created 4 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
oddeirikcommented, Feb 7, 2020

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! 😃

1reaction
josh-degrawcommented, Dec 17, 2019

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 referencing NLog.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.:

 SentrySdk.ConfigureScope(s => s.User.Username = theUserNameEtc);

? 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Identify Users for NLog
Learn how to configure the SDK to capture the user and gain critical pieces of information that construct a unique identity in Sentry....
Read more >
NLog - Render username on the current request
I am using AspNetCore.Identity for authentication/authorization and I found out that I can log as well the requester's username by including ${ ...
Read more >
Config options for NLog's configuration
AspNetBufferingWrapper - Buffers log events for the duration of ASP.NET request and sends them down to the wrapped target at the end of...
Read more >
Logging in .NET Core and ASP.NET Core
Log event ID. Each log can specify an event ID. The sample app uses the MyLogEvents class to define event IDs: C#
Read more >
Sentry.NLog 3.17.0
Official NLog integration for Sentry - Open-source error tracking that helps developers monitor and fix crashes in real time.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found