-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
75 lines (66 loc) · 2.28 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using CliFx;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using Serilog.Events;
namespace AutoFocus;
internal class Program
{
private static void SetupLogger()
{
#if DEBUG
// ReSharper disable once ConvertToConstant.Local
var logFilePath = "log.txt";
#else
string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string appFolder = Path.Combine(appDataPath, "AutoFocus");
string logFolder = Path.Combine(appFolder, "Logs");
if (!Directory.Exists(logFolder))
{
Directory.CreateDirectory(logFolder);
}
string logFilePath = Path.Combine(logFolder, "log.txt");
#endif
// Configure Serilog
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.Enrich.FromLogContext()
.WriteTo.Async(sink => sink.File(logFilePath, rollingInterval: RollingInterval.Day, rollOnFileSizeLimit: true))
.WriteTo.Console(restrictedToMinimumLevel: LogEventLevel.Information)
.CreateLogger();
}
public static async Task<int> Main()
{
int returnCode;
SetupLogger();
try
{
returnCode = await new CliApplicationBuilder()
.SetTitle("AutoFocus")
.SetDescription("Automate changing Sample Rate and Buffer Size in Focusrite Notifier device settings.")
.SetExecutableName("autofocus.exe")
.AddCommandsFromThisAssembly()
.UseTypeActivator(commandTypes =>
{
var services = new ServiceCollection();
services.AddLogging(builder => builder.AddSerilog(dispose: true));
// Register all commands as transient services
foreach (var commandType in commandTypes)
services.AddTransient(commandType);
return services.BuildServiceProvider();
})
.AllowDebugMode()
.Build()
.RunAsync();
}
catch (Exception ex)
{
returnCode = 1;
Log.Error(ex, "Something went wrong");
}
finally
{
await Log.CloseAndFlushAsync();
}
return returnCode;
}
}