Skip to content

Commit 84c4a07

Browse files
add first argument to configure Loglevel
1 parent 376e28f commit 84c4a07

2 files changed

Lines changed: 28 additions & 8 deletions

File tree

FileSyncApp/Program.cs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public class Program
2525
public static Dictionary<string, IFileJob> Jobs = new Dictionary<string, IFileJob>();
2626
public static void Main(string[] args)
2727
{
28-
ConfigureLogger();
28+
ConfigureLogger(args.FirstOrDefault());
2929
log = LoggerFactory.CreateLogger("FileSyncAppMain");
3030
if (null != args && args.Length > 0)
3131
{
@@ -71,7 +71,15 @@ static void RunProgram()
7171
.WithInterval(TimeSpan.FromMinutes(10) + TimeSpan.FromSeconds(25))
7272
.SyncRecursive(true)
7373
.Build();
74+
7475
jobOptions.Add("SyncFromEdgeToLocal", syncFromEdgeToLocal);
76+
var clean = FileCleanJobOptionsBuilder.CreateBuilder()
77+
.WithDestinationPath("\\\\sbrv\\share\\folder")
78+
.WithFileSyncProvider(SyncProvider.FileIO)
79+
.WithMaxAge(TimeSpan.FromDays(30))
80+
.Build();
81+
82+
jobOptions.Add("cleanJob", clean);
7583

7684
var hostname = Dns.GetHostName();
7785

@@ -111,14 +119,22 @@ static void RunProgram()
111119
}
112120

113121

114-
public static void ConfigureLogger()
122+
public static void ConfigureLogger(string logLevel)
115123
{
116-
if (LoggingLevel != null)
117-
return;
124+
Exception logLevelException = null;
118125
LoggingLevel = new LoggingLevelSwitch(Serilog.Events.LogEventLevel.Information);
119-
#if DEBUG
120-
LoggingLevel = new LoggingLevelSwitch(Serilog.Events.LogEventLevel.Verbose);
121-
#endif
126+
try
127+
{
128+
if (!string.IsNullOrEmpty(logLevel))
129+
{
130+
LoggingLevel = new LoggingLevelSwitch((Serilog.Events.LogEventLevel)Enum.Parse(typeof(Serilog.Events.LogEventLevel), logLevel));
131+
}
132+
}
133+
catch (Exception exc)
134+
{
135+
logLevelException = exc;
136+
}
137+
122138
string serilogFileTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {SourceContext:l} {Message:lj}{NewLine}{Exception}";
123139
string serilogConsoleTemplate = "{Timestamp:HH:mm:ss.fff}[{Level:u3}]{SourceContext:l} {Message:lj}{NewLine}{Exception}";
124140
Serilog.Log.Logger = new LoggerConfiguration()
@@ -136,6 +152,10 @@ public static void ConfigureLogger()
136152
.CreateLogger();
137153

138154
LoggerFactory = Microsoft.Extensions.Logging.LoggerFactory.Create(builder => { builder.AddSerilog(Serilog.Log.Logger); });
155+
if (null != logLevelException)
156+
{
157+
Serilog.Log.Error(logLevelException, "exception setting log level to {A}", logLevel);
158+
}
139159
}
140160

141161

FileSyncAppWin/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal static class Program
1818
[STAThread]
1919
static void Main(string[] args)
2020
{
21-
FileSyncApp.Program.ConfigureLogger();
21+
FileSyncApp.Program.ConfigureLogger(args.FirstOrDefault());
2222
logger = FileSyncApp.Program.LoggerFactory.CreateLogger("FileSyncAppWin");
2323
if (args.Contains("noautorestart"))
2424
{

0 commit comments

Comments
 (0)