Skip to content

Commit 0f1db48

Browse files
committed
Added more logging options
1 parent 71c23a4 commit 0f1db48

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

src/CodeUpdater/CodeUpdater/CodeUpdater.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<PackageReference Include="CommandLineParser" Version="2.9.1" />
2929
<PackageReference Include="ProgrammerAl.SourceGenerators.PublicInterfaceGenerator" Version="1.0.0.62" PrivateAssets="All" />
3030
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
31+
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
3132
</ItemGroup>
3233

3334
<ItemGroup>

src/CodeUpdater/CodeUpdater/Options/CommandOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@ public class CommandOptions
1313
{
1414
[Option(shortName: 'c', longName: "config-file", Required = true, HelpText = "Path to the file to use for config values when updating code")]
1515
public required string ConfigFile { get; set; }
16+
17+
[Option(shortName: 'o', longName: "output-file", Required = false, HelpText = "If this is set, it will be the file to write logs to.")]
18+
public string? OutputFile { get; set; }
19+
20+
[Option(shortName: 'l', longName: "log-level", Required = false, HelpText = "Level to log. Valid values are: Verbose, Info, Warn, Error. Default value is verbose.")]
21+
public string LogLevel { get; set; } = "verbose";
1622
}

src/CodeUpdater/CodeUpdater/Program.cs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,17 @@
1414

1515
using Serilog;
1616

17-
Log.Logger = new LoggerConfiguration()
18-
.WriteTo.Console()
19-
.CreateLogger();
20-
2117
await Parser.Default.ParseArguments<CommandOptions>(args)
2218
.WithParsedAsync(async options =>
2319
{
2420
await RunAsync(options);
2521
});
2622

27-
static async ValueTask RunAsync(CommandOptions optionOptions)
23+
static async ValueTask RunAsync(CommandOptions options)
2824
{
29-
var logger = Log.Logger;
25+
var logger = SetupLogger(options);
3026

31-
var updateOptions = await LoadUpdateOptionsAsync(optionOptions.ConfigFile);
27+
var updateOptions = await LoadUpdateOptionsAsync(options.ConfigFile);
3228

3329
var runProcessHelper = new RunProcessHelper(logger);
3430
var workLocator = new WorkLocator(logger);
@@ -45,6 +41,7 @@ static async ValueTask RunAsync(CommandOptions optionOptions)
4541

4642
if (!canRun)
4743
{
44+
logger.Fatal($"Cannot run, exiting before trying to do any work...");
4845
return;
4946
}
5047

@@ -59,6 +56,36 @@ static async ValueTask RunAsync(CommandOptions optionOptions)
5956
OutputSummary(updateWork, csUpdates, npmUpdates, compileResults, logger);
6057
}
6158

59+
static ILogger SetupLogger(CommandOptions options)
60+
{
61+
var loggerConfig = new LoggerConfiguration()
62+
.WriteTo.Console();
63+
64+
if (!string.IsNullOrWhiteSpace(options.OutputFile))
65+
{
66+
loggerConfig = loggerConfig.WriteTo.File(options.OutputFile);
67+
}
68+
69+
if (string.IsNullOrWhiteSpace(options.LogLevel))
70+
{
71+
loggerConfig = loggerConfig.MinimumLevel.Verbose();
72+
}
73+
else
74+
{
75+
loggerConfig = options.LogLevel.ToLower() switch
76+
{
77+
"verbose" => loggerConfig.MinimumLevel.Verbose(),
78+
"info" => loggerConfig.MinimumLevel.Information(),
79+
"warn" => loggerConfig.MinimumLevel.Warning(),
80+
"error" => loggerConfig.MinimumLevel.Error(),
81+
_ => loggerConfig.MinimumLevel.Verbose(),
82+
};
83+
}
84+
85+
Log.Logger = loggerConfig.CreateLogger();
86+
return Log.Logger;
87+
}
88+
6289
static async Task<UpdateOptions> LoadUpdateOptionsAsync(string configFilePath)
6390
{
6491
if (!File.Exists(configFilePath))

0 commit comments

Comments
 (0)