Modifying log messages before sending them to Application Insights #6613
Unanswered
Kalasklister
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi!
I'm using OpenTelemetry logging with Azure Monitor Application Insights in my ASP.NET Core project. Every time my app logs anything, it's done with one of these two methods: ILogger.LogInformation("some message") or ILogger.LogError(new Exception("some message").
I want to guard against the (slim) risk that "some message" contains a social security number and that it gets logged to application insights. Therefore, I want to censor social security numbers in all log messages by replacing the original message with a copy where the social security number is censored.
I've managed to do this for information logs, but not with exception logs. I've tried many things, but a custom log processor added to the LoggingProviderBuilder is as far as my understanding goes the preffered way of modifying logs.
What I've done in my custom log processor works very well for logs logged with ILogger.LogInformation(), but for logs logged with ILogger.LogError() - since messages can not be changed in Exceptions - the only solution I've managed to get to work is to completely replace the exception with a new one that has the modified message. This has drawbacks: the stack trace is lost, and I feel like creating a new exception is in general not a neat solution.
Is there any other way of acheiving my goal? I'm very greatful for any thoughts, ideas, tips, etc!
(I've also tried creating a wrapper around the OpenTelemetryLogger, but the issue seems to be the same - I must replace the exception with a new one.)
Here is my code:
Custom log processor:
program.cs:
Beta Was this translation helpful? Give feedback.
All reactions