Skip to content

Commit

Permalink
Modified file logging to utilize persistent file, and added default f…
Browse files Browse the repository at this point in the history
…or sleep time
  • Loading branch information
toutas committed Dec 25, 2021
1 parent acd8125 commit b0af710
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Models/RCloneConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
public class RCloneConfig
{
[YamlDotNet.Serialization.YamlMember(Alias = "sleeptime")]
public int SleepTime { get; set; }
public int SleepTime { get; set; } = 5;
}
}
2 changes: 1 addition & 1 deletion Models/SARotateConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace SARotate.Models
public class SARotateConfig
{
[YamlMember(Alias = "rclone")]
public RCloneConfig RCloneConfig { get; set; }
public RCloneConfig RCloneConfig { get; set; } = new RCloneConfig();

[YamlMember(Alias = "remotes")]
public Dictionary<string, Dictionary<string, RemoteInfo>> RemoteConfig { get; set; }
Expand Down
36 changes: 15 additions & 21 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,9 @@ private static async Task Main(string[] args)
cts.Cancel();
};

//bool validRcloneVersion = await CheckValidRcloneVersion();
//if (!validRcloneVersion)
//{
// Console.WriteLine("Rclone versions below v1.55 are unsupported");
// cts.Cancel();
//}

using IHost host = CreateHostBuilder(args, cts).Build();

Log.Information("SARotate Version 2.0.2 started");
Log.Information("SARotate Version 2.0.3 started");

await host.RunAsync(cts.Token);
Log.CloseAndFlush();
Expand All @@ -79,6 +72,7 @@ private static IHostBuilder CreateHostBuilder(string[] args, CancellationTokenSo
Environment.Exit(-1);
}


Logger logger = CreateLogger(cwd, logFilePath, verboseFlagExists);

return Host.CreateDefaultBuilder()
Expand Down Expand Up @@ -140,17 +134,17 @@ private static Logger CreateLogger(string cwd, string? logFilePath, bool verbose
int retainedFileCountLimit = int.Parse(_configuration["Serilog:WriteTo:0:Args:configure:0:Args:retainedFileCountLimit"] ?? "5");

LogEventLevel minimumLogEventLevel = ConvertMinimumLogLevelConfigToLogEventLevel(minimumLogLevelConfig);
RollingInterval rollingInterval = ConvertRollingIntervalConfigValueToEnum(rollingIntervalConfig);
PersistentFileRollingInterval rollingInterval = ConvertRollingIntervalConfigValueToEnum(rollingIntervalConfig);

Logger logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.Enrich.WithProperty("Application", "SARotate")
.Enrich.With<GenericLogEnricher>()
.MinimumLevel.ControlledBy(new LoggingLevelSwitch(minimumLogEventLevel))
.WriteTo.Async(a => a.File(logPath,
fileSizeLimitBytes: fileSizeLimitBytes,
rollingInterval: rollingInterval,
retainedFileCountLimit: retainedFileCountLimit))
.WriteTo.PersistentFile(logPath,
fileSizeLimitBytes: fileSizeLimitBytes,
persistentFileRollingInterval: rollingInterval,
retainedFileCountLimit: retainedFileCountLimit)
.WriteTo.Async(a => a.Console(outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:j}{NewLine}{Exception}"))
.CreateLogger();

Expand All @@ -159,17 +153,17 @@ private static Logger CreateLogger(string cwd, string? logFilePath, bool verbose
return logger;
}

private static RollingInterval ConvertRollingIntervalConfigValueToEnum(string rollingInterval)
private static PersistentFileRollingInterval ConvertRollingIntervalConfigValueToEnum(string rollingInterval)
{
return rollingInterval.ToLower() switch
{
"infinite" => RollingInterval.Infinite,
"year" => RollingInterval.Year,
"month" => RollingInterval.Month,
"day" => RollingInterval.Day,
"hour" => RollingInterval.Hour,
"minute" => RollingInterval.Minute,
_ => RollingInterval.Day
"infinite" => PersistentFileRollingInterval.Infinite,
"year" => PersistentFileRollingInterval.Year,
"month" => PersistentFileRollingInterval.Month,
"day" => PersistentFileRollingInterval.Day,
"hour" => PersistentFileRollingInterval.Hour,
"minute" => PersistentFileRollingInterval.Minute,
_ => PersistentFileRollingInterval.Day
};
}

Expand Down
1 change: 1 addition & 0 deletions SARotate.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<PackageReference Include="Serilog.Sinks.Async" Version="1.4.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0" />
<PackageReference Include="Serilog.Sinks.PersistentFile" Version="4.1.0-dev-00014" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0" />
<PackageReference Include="YamlDotNet" Version="11.1.1" />
</ItemGroup>
Expand Down

0 comments on commit b0af710

Please sign in to comment.