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
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions SteamTogether.Bot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using SteamTogether.Bot.Services.Command.Handlers;
using SteamTogether.Bot.Services.Command.Parser;
using SteamTogether.Core;
using SteamTogether.Core.Logging;
using SteamTogether.Core.Options;
using SteamTogether.Core.Services.Steam;
using Telegram.Bot;
Expand All @@ -25,6 +26,51 @@
.AddCommandLine(args);
}
)
.ConfigureLogging(
(context, logging) =>
{
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

{
if (level == LogLevel.None)
{
return false;
}

if (category == typeof(SteamService).FullName)
{
return level == LogLevel.Information;
}

if (category == typeof(TelegramService).FullName)
{
return level == LogLevel.Information;
}

if (category == typeof(HealthCheckService).FullName)
{
return level == LogLevel.Information;
}

if (category == typeof(TelegramPollingWorker).FullName)
{
return level == LogLevel.Information;
}

if (category == typeof(HealthCheckWorker).FullName)
{
return level == LogLevel.Information;
}

return true;
},
true
)
);
}
)
.ConfigureServices(
(builder, services) =>
{
Expand Down
88 changes: 88 additions & 0 deletions SteamTogether.Core/Logging/FilteredConsoleLoggerProvider.cs
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", "***");
}
}