Skip to content
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
wants to merge 2 commits into from

Conversation

Zebradil
Copy link
Contributor

No description provided.

@Zebradil Zebradil self-assigned this Apr 17, 2023
@Zebradil Zebradil linked an issue Apr 17, 2023 that may be closed by this pull request
@Zebradil Zebradil added the enhancement New feature or request label Apr 17, 2023
@Zebradil
Copy link
Contributor Author

@ablizorukov I'm not sure if I'm moving in the right direction. Do you have experience implementing logging middleware/handler/provider? We need to modify the output before sending it to the receiver (console in the current case).

logging.ClearProviders();
logging.AddProvider(
new FilteredConsoleLoggerProvider(
(category, level) =>
Copy link
Contributor

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

Copy link
Contributor Author

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.

Copy link
Contributor

@ablizorukov ablizorukov Apr 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

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.

Copy link
Contributor

@ablizorukov ablizorukov Apr 20, 2023

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

{ 
 "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.Hosting.Lifetime": "Information",
      "System.Net.Http.HttpClient.TelegramBotClient": "Warning"
    }
}

Also possible to check out third-party libraries like log4net, serilog and etc, it might be easier to implement replace sensitive log messages

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will affect all console output and change the appearens

That might be not a bad thing. This way we'll make sure that the token won't appear in any logs.

just change the logging level

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.

check out third-party libraries

That'd be fine for me if you pick something. I have little experience in C# and limited knowledge of the ecosystem.

Copy link
Contributor

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

@Zebradil Zebradil closed this Apr 20, 2023
@Zebradil Zebradil deleted the 35-bot-logs-contains-telegram-token-1 branch April 20, 2023 19:50
@Zebradil Zebradil restored the 35-bot-logs-contains-telegram-token-1 branch April 20, 2023 19:50
@Zebradil Zebradil reopened this Apr 20, 2023
@github-actions
Copy link

Code Coverage

Package Line Rate Branch Rate Health
SteamTogether.Bot 3% 5%
SteamTogether.Scraper 63% 58%
SteamTogether.Core 6% 12%
Summary 12% (186 / 1492) 20% (18 / 90)

@sonarcloud
Copy link

sonarcloud bot commented Apr 21, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 4 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@Zebradil Zebradil removed their assignment Jun 18, 2024
@Zebradil Zebradil closed this Jun 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

⚠️ Bot logs contains Telegram Token
2 participants