Skip to content

Commit

Permalink
Added ability to change timestamp format in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
dotnetdummy committed Apr 11, 2024
1 parent 2814115 commit 85fcce6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dotnet publish -c Release -r linux-x64 /p:PublishSingleFile=true /p:PublishTrimm

## Environment variables

- `LOGGING__TIMESTAMPFORMAT`: Timestamp format for logs. Default is `[yyyy-MM-dd HH:mm:ss.ffffffzzzz]`.
- `CORE__FILECOMPAREINTERVAL`: To determine if the file has been fully copied, the length of the file is compared between a given interval. If the lengths are equal, then the copy process starts. Default is 15 seconds. Must be 1 or greater.
- `PATHS__SOURCE`: **(required)** Source directory to watch for new files.
- `PATHS__WHITELISTEDWORDS`: Comma separated list of words to whitelist files in source directory. If not set, all files will be processed.
Expand Down
19 changes: 14 additions & 5 deletions TorrentExtractor/CustomLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ public static ILoggingBuilder AddCustomFormatter(this ILoggingBuilder builder) =
builder
.AddConsole(options => options.FormatterName = nameof(CustomLoggingFormatter))
.AddConsoleFormatter<CustomLoggingFormatter, ConsoleFormatterOptions>();

public static ILoggingBuilder AddCustomFormatter(
this ILoggingBuilder builder,
Action<ConsoleFormatterOptions> configure
) =>
builder
.AddConsole(options => options.FormatterName = nameof(CustomLoggingFormatter))
.AddConsoleFormatter<CustomLoggingFormatter, ConsoleFormatterOptions>(configure);
}

public sealed class CustomLoggingFormatter : ConsoleFormatter, IDisposable
Expand Down Expand Up @@ -43,12 +51,13 @@ TextWriter textWriter
return;
}

var tsFormat = _formatterOptions.TimestampFormat ?? "[yyyy-MM-dd HH:mm:ss.ffffffzzzz]";

textWriter.WriteLine(
$"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {GetLogLevelString(logEntry.LogLevel)}: {message}{
(logEntry.Exception != null ? $". {logEntry.Exception?.GetType()}: {logEntry.Exception?.Message}" : "")}".Replace(
"..",
"."
)
$"{(tsFormat == "" ? "" : DateTime.UtcNow.ToString(tsFormat))} {GetLogLevelString(logEntry.LogLevel)}: {message}{
(logEntry.Exception != null ? $". {logEntry.Exception?.GetType()}: {logEntry.Exception?.Message}" : "")}"
.Replace("..", ".")
.Trim()
);
}

Expand Down
6 changes: 5 additions & 1 deletion TorrentExtractor/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

Expand All @@ -18,7 +19,10 @@ private static IHostBuilder CreateHostBuilder(string[] args) =>
{
services.AddLogging(opt =>
{
opt.AddCustomFormatter();
var format = hostContext.Configuration.GetValue<string>(
"Logging:TimestampFormat"
);
opt.AddCustomFormatter(conf => conf.TimestampFormat = format);
});
services.AddHostedService<Worker>();
services.AddOptions();
Expand Down

0 comments on commit 85fcce6

Please sign in to comment.