-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom logging provider to modify output #40
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
SteamTogether.Core/Logging/FilteredConsoleLoggerProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace SteamTogether.Core.Logging; | ||
|
||
public class FilteredConsoleLoggerProvider : ILoggerProvider | ||
{ | ||
private readonly Func<string, LogLevel, bool> _filter; | ||
private readonly bool _filterSensitiveData; | ||
|
||
public FilteredConsoleLoggerProvider( | ||
Func<string, LogLevel, bool> filter, | ||
bool filterSensitiveData = false | ||
) | ||
{ | ||
_filter = filter; | ||
_filterSensitiveData = filterSensitiveData; | ||
} | ||
|
||
public ILogger CreateLogger(string categoryName) | ||
{ | ||
return new FilteredConsoleLogger(categoryName, _filter, _filterSensitiveData); | ||
} | ||
|
||
public void Dispose() { } | ||
} | ||
|
||
public class FilteredConsoleLogger : ILogger | ||
{ | ||
private readonly string _categoryName; | ||
private readonly Func<string, LogLevel, bool> _filter; | ||
private readonly bool _filterSensitiveData; | ||
|
||
public FilteredConsoleLogger( | ||
string categoryName, | ||
Func<string, LogLevel, bool> filter, | ||
bool filterSensitiveData | ||
) | ||
{ | ||
_categoryName = categoryName; | ||
_filter = filter; | ||
_filterSensitiveData = filterSensitiveData; | ||
} | ||
|
||
public IDisposable? BeginScope<TState>(TState state) | ||
where TState : notnull => default!; | ||
|
||
public bool IsEnabled(LogLevel logLevel) | ||
{ | ||
return _filter(_categoryName, logLevel); | ||
} | ||
|
||
void ILogger.Log<TState>( | ||
LogLevel logLevel, | ||
EventId eventId, | ||
TState state, | ||
Exception? exception, | ||
Func<TState, Exception?, string> formatter | ||
) | ||
{ | ||
if (!IsEnabled(logLevel)) | ||
{ | ||
return; | ||
} | ||
|
||
if (_filterSensitiveData) | ||
{ | ||
state = (TState)FilterSensitiveData(state); | ||
} | ||
|
||
Console.WriteLine($"{logLevel}: {formatter(state, exception)}"); | ||
} | ||
|
||
private static object FilterSensitiveData<TState>(TState state) | ||
{ | ||
if (state is string) | ||
{ | ||
return FilterStringSensitiveData((string)(object)state); | ||
} | ||
|
||
return state; | ||
} | ||
|
||
private static string FilterStringSensitiveData(string message) | ||
{ | ||
// Replace any occurrences of "password" with "***" | ||
return message.Replace("password", "***"); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This levels should not be hardcoded, it can be configured from the appsettings file
We can define default level as Warning and set other need logging as Information
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-7.0#configure-logging
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes-yes, it's generated code.
I'd like to avoid implementing level filtering logic at all. It is already implemented in the built-in logger, we just need to hijack formatting somehow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I see the problem maybe RedactLoggedHeaders method for TelegramBotClient can help here, but will need to check
https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.httpclientbuilderextensions.redactloggedheaders?view=dotnet-plat-ext-7.0&viewFallbackFrom=net-7.0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it won't help. In our case the URL gets logged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should do: Create new Custom ConsoleLogFormatter
But this will affect all console output and change the appearens, in our case I would just change the logging level by default to something like:
// appsettings.bot.json
Also possible to check out third-party libraries like log4net, serilog and etc, it might be easier to implement replace sensitive log messages
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That might be not a bad thing. This way we'll make sure that the token won't appear in any logs.
I'm inclined to do that now as a quick mitigation, but this won't fix the root cause.
It is quite surprising that no straightforward ways to mutate log output can be googled. In my experience, it is required not often but from time to time.
That'd be fine for me if you pick something. I have little experience in C# and limited knowledge of the ecosystem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I'll take a look at 3rd party libs