You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi everyone, I have a (maybe) dummy question. I'm using Sentry for Asp.NET using as logging methods the one shipped in the framework via Microsoft.Extensions.Logging
I'm logging errors and exceptions passing sometimes parameters like in this way:
_logger.LogError("ERROR - User Registration Failed Invalid Recaptcha",new{RegisterModel=registerModel,RecaptchaResponse=recaptchaResponse});
The question is: Where am I supposed to see these parameters in the Sentry interface? I mean, logging is working pretty fine, especially with the exceptions caught. I see everything I need; I just never see, in any case, those parameters.
Am I doing something wrong, or is this the expected behavior?
Expected Result
I would expect to see the parameters as a dictionary or a deserialized object in Sentry Event
Actual Result
Nothing about those parameters is shown on the event details
Hm, typically you log errors when you have an exception, in which case the exception can be passed as well and you get full stack trace details etc. on the Issues dashboard in Sentry):
So basically the State has to be an IEnumerable<KeyValuePair<string, object>> and the values can be string, Guid, Enum or some primitive (like int). If you want to pass a complex model like RegisterModel you'd need to first serialize this to JSON or something (so that it could be supplied as a string).
Hello @jamescrosswell I see your point; that's said, the solution is not very user-friendly.
Besides that, there are plenty of cases in which you want to log an error without an exception, like in those cases where you have logic errors. That's why the LogError of ILogger has a method that doesn't need an exception to log the data.
Anyway, if there is no other way, I will give it a try with the scopes way. Many thanks for the help
I don't think there's any other way that Sentry could get data from the logger. In the code you provided above, you're making a call to this extension method:
/// <summary>/// Formats and writes an error log message./// </summary>/// <param name="logger">The <see cref="ILogger"/> to write to.</param>/// <param name="message">Format string of the log message in message template format. Example: <c>"User {User} logged in from {Address}"</c></param>/// <param name="args">An object array that contains zero or more objects to format.</param>/// <example>logger.LogError("Processing request from {Address}", address)</example>publicstaticvoidLogError(thisILoggerlogger,string?message,paramsobject?[]args){logger.Log(LogLevel.Error,message,args);}
So the logger doesn't do anything with registerModel and recaptchaResponse. The params args in that extension method would only ever be used to format the message (if you were using message templating).
If you can think of an easier way to pass data from the logger to Sentry, we could look into it though. We're always looking for ways to make Sentry easier to use.
@jamescrosswell you are totally and absolutely right, i don't know why I was thinking that it worked in another way, perhaps I was using another library.
Surely the way you mentioned could be to pass everything as parameters and then stringify those as parameters in the message, but at that point I would have everything in a single-line string on Sentry I suppose. It could help, but for bigger objects, it will be tricky.
In the other case, I could also create a custom Exception using the Data Property: Exception.Data property to add there my custom objects as properties; those surely would be available in the Sentry Dashboard, right?
I don't know if you want to give it some thought; if there could be an easier way, it could be a point of improvement
Hello @jamescrosswell any news on this topic? I keep receiving logs without precious information in them. Having a stack trace is useful, but if the data from a form, generates decisions in your code, and you cannot see that data, it's not useful in this case
Many thanks
Hello @jamescrosswell any news on this topic? I keep receiving logs without precious information in them. Having a stack trace is useful, but if the data from a form, generates decisions in your code, and you cannot see that data, it's not useful in this case
Hello @jamescrosswell yes but this works only for simple types. What if you have 40 parameters? Surely you can JsonSerializer.Serialize everything there, but you will have a whole chunk of parameters in the message. It's not very user-friendly.
At least you can do the same as I said in the comment but the interface won't show you everything as I told you in that message
Activity
jamescrosswell commentedon Jan 24, 2024
Hm, typically you log errors when you have an exception, in which case the exception can be passed as well and you get full stack trace details etc. on the Issues dashboard in Sentry):
sentry-dotnet/samples/Sentry.Samples.ME.Logging/Program.cs
Lines 65 to 73 in d6dc82b
If you wanted additional information to be sent to Sentry, one way to do it would be via the logger state:
sentry-dotnet/samples/Sentry.Samples.ME.Logging/Program.cs
Lines 51 to 59 in d6dc82b
Under the hood that information gets passed through to Sentry like so:
sentry-dotnet/src/Sentry.Extensions.Logging/SentryLogger.cs
Lines 113 to 135 in 464fd4c
So basically the State has to be an
IEnumerable<KeyValuePair<string, object>>and the values can bestring,Guid,Enumor some primitive (likeint). If you want to pass a complex model likeRegisterModelyou'd need to first serialize this to JSON or something (so that it could be supplied as a string).ollie10 commentedon Jan 24, 2024
Hello @jamescrosswell I see your point; that's said, the solution is not very user-friendly.
Besides that, there are plenty of cases in which you want to log an error without an exception, like in those cases where you have logic errors. That's why the LogError of ILogger has a method that doesn't need an exception to log the data.
Anyway, if there is no other way, I will give it a try with the scopes way. Many thanks for the help
jamescrosswell commentedon Jan 24, 2024
Hey @ollie10 ,
I don't think there's any other way that Sentry could get data from the logger. In the code you provided above, you're making a call to this extension method:
So the logger doesn't do anything with
registerModelandrecaptchaResponse. Theparams argsin that extension method would only ever be used to format themessage(if you were using message templating).If you can think of an easier way to pass data from the logger to Sentry, we could look into it though. We're always looking for ways to make Sentry easier to use.
ollie10 commentedon Jan 24, 2024
@jamescrosswell you are totally and absolutely right, i don't know why I was thinking that it worked in another way, perhaps I was using another library.
Surely the way you mentioned could be to pass everything as parameters and then stringify those as parameters in the message, but at that point I would have everything in a single-line string on Sentry I suppose. It could help, but for bigger objects, it will be tricky.
I don't know if it could be helpful to adopt a solution like this one: https://github.com/serilog/serilog/wiki/Structured-Data or some similar thing.
In the other case, I could also create a custom Exception using the Data Property: Exception.Data property to add there my custom objects as properties; those surely would be available in the Sentry Dashboard, right?
I don't know if you want to give it some thought; if there could be an easier way, it could be a point of improvement
14 remaining items
ollie10 commentedon Feb 19, 2024
Hello @jamescrosswell any news on this topic? I keep receiving logs without precious information in them. Having a stack trace is useful, but if the data from a form, generates decisions in your code, and you cannot see that data, it's not useful in this case
Many thanks
jamescrosswell commentedon Feb 19, 2024
Hey @ollie10, did you check the sample project illustrating this? Is that not the behaviour you're after?
ollie10 commentedon Feb 19, 2024
Hello @jamescrosswell yes but this works only for simple types. What if you have 40 parameters? Surely you can JsonSerializer.Serialize everything there, but you will have a whole chunk of parameters in the message. It's not very user-friendly.
At least you can do the same as I said in the comment but the interface won't show you everything as I told you in that message
jamescrosswell commentedon Feb 19, 2024
I'm not sure if you saw the comment in the code for the sample:
So if you're using structured message templates with MEL, Sentry creates tags for each of the parameters passed to the message template.
Is that not what you're after?