Skip to content

Commit

Permalink
🔧 centralize log path creation and improve logging (#2385)
Browse files Browse the repository at this point in the history
- change the rolling interval on the main log to hourly, and to
rollOnFileSizeLimit
- change the rolling interval on the error log to daily
- Added the log path to the output, beginning and end.
  • Loading branch information
MrHinsh authored Sep 23, 2024
2 parents 822ef81 + 28ff730 commit 2a81288
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 26 deletions.
16 changes: 8 additions & 8 deletions docs/Reference/Generated/MigrationTools.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/MigrationTools.Host/Commands/CommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public sealed override async Task<int> ExecuteAsync(CommandContext context, TSet
Lifetime.StopApplication();
CommandActivity?.Stop();
_logger.LogInformation("Command {CommandName} completed in {Elapsed}", this.GetType().Name, CommandActivity?.Duration);
_logger.LogInformation("Check the logs for errors: {LogPath}", LogLocationService.GetLogPath());
}

}
Expand Down Expand Up @@ -196,6 +197,7 @@ private void ApplicationStartup(TSettings settings)
_logger.LogInformation("Running with settings: {@settings}", settings);
_logger.LogInformation("OSVersion: {OSVersion}", Environment.OSVersion.ToString());
_logger.LogInformation("Version (Assembly): {Version}", _MigrationToolVersion.GetRunningVersion().versionString);
_logger.LogInformation("Logpath: {LogPath}", LogLocationService.GetLogPath());
}


Expand Down
21 changes: 3 additions & 18 deletions src/MigrationTools.Host/MigrationToolHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static IHostBuilder CreateDefaultBuilder(string[] args, Action<IConfigura
var configFile = CommandSettingsBase.ForceGetConfigFile(args);
var mtv = new MigrationToolVersion();

string logsPath = CreateLogsPath();
string logsPath = LogLocationService.GetLogPath();

var hostBuilder = Microsoft.Extensions.Hosting.Host.CreateDefaultBuilder(args);

Expand All @@ -66,8 +66,8 @@ public static IHostBuilder CreateDefaultBuilder(string[] args, Action<IConfigura
.Enrich.WithProperty("versionString", mtv.GetRunningVersion().versionString)
.Enrich.FromLogContext()
.Enrich.WithProcessId()
.WriteTo.File(Path.Combine(logsPath, $"migration.log"), LogEventLevel.Verbose, shared: true,outputTemplate: outputTemplate)
.WriteTo.File(new Serilog.Formatting.Json.JsonFormatter(), Path.Combine(logsPath, $"migration-errors.log"), LogEventLevel.Error, shared: true)
.WriteTo.File(Path.Combine(logsPath, $"migration.log"), LogEventLevel.Verbose, shared: true,outputTemplate: outputTemplate, rollOnFileSizeLimit: true, rollingInterval: RollingInterval.Hour)
.WriteTo.File(new Serilog.Formatting.Json.JsonFormatter(), Path.Combine(logsPath, $"migration-errors.log"), LogEventLevel.Error, shared: true, rollOnFileSizeLimit: true, rollingInterval: RollingInterval.Day)
.WriteTo.Logger(lc => lc
.Filter.ByExcluding(Matching.FromSource("Microsoft.Hosting.Lifetime"))
.Filter.ByExcluding(Matching.FromSource("Microsoft.Extensions.Hosting.Internal.Host"))
Expand Down Expand Up @@ -141,20 +141,5 @@ public static IHostBuilder CreateDefaultBuilder(string[] args, Action<IConfigura
return hostBuilder;
}

static string logDate = DateTime.Now.ToString("yyyyMMddHHmmss");

private static string CreateLogsPath()
{
string exportPath;
string assPath = Assembly.GetEntryAssembly().Location;
exportPath = Path.Combine(Path.GetDirectoryName(assPath), "logs", logDate);
if (!Directory.Exists(exportPath))
{
Directory.CreateDirectory(exportPath);
}

return exportPath;
}

}
}
42 changes: 42 additions & 0 deletions src/MigrationTools/Services/LogLocationService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

namespace MigrationTools.Services
{
public class LogLocationService
{
private static string logDate = DateTime.Now.ToString("yyyyMMddHHmmss");


private static string _logsPath = CreateLogsPath();

public static string GetLogPath()
{
// Check if the string has been generated.
if (_logsPath == null)
{
// Generate the string at runtime (e.g., any custom logic).
_logsPath = CreateLogsPath();
}

// Return the _logsPath string.
return _logsPath;
}

private static string CreateLogsPath()
{
string exportPath;
string assPath = Assembly.GetEntryAssembly().Location;
exportPath = Path.Combine(Path.GetDirectoryName(assPath), "logs", logDate);
if (!Directory.Exists(exportPath))
{
Directory.CreateDirectory(exportPath);
}

return exportPath;
}
}
}

0 comments on commit 2a81288

Please sign in to comment.