/// <summary> /// The main application entry point. /// </summary> /// <param name="args">The arguments.</param> /// <returns>A Task.</returns> public static async Task Main(string[] args) { var logconfig = new Logging.LoggingConfig { LogFolder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Logs"), LogfilePrefix = "PCDL", FilesToKeep = 20, MinLogLevel = Logging.LogLevel.Debug, }; LoggerSingleton.Initialize(logconfig); #if !DEBUG logconfig.MinLogLevel = LogLevel.Information; #endif while (true) { LoggerSingleton.Value.Log(LogLevel.Information, "Main", "Version " + typeof(Program).Assembly.GetName().Version.ToString()); await ProcessConfig(LocalPath); LoggerSingleton.Value.Cleanup(); #if DEBUG break; #else await Task.Delay(TimeSpan.FromHours(12)); #endif } }
/// <summary> /// Initializes a new instance of the <see cref="Logger"/> class. /// </summary> /// <param name="config">The configuration.</param> /// <exception cref="ArgumentNullException">config cannot be null.</exception> public Logger(LoggingConfig config) { if (config is null) { throw new ArgumentNullException(nameof(config)); } configuration = config; this.MinLogLevel = config.MinLogLevel; this.Initialize(config); }
/// <summary> /// Initializes a new instance of the <see cref="LogWriter" /> class. /// </summary> /// <param name="config">The logger configuration.</param> public LogWriter(LoggingConfig config) { if (config is null) { throw new ArgumentNullException(nameof(config)); } this.filePrefix = config.LogfilePrefix; this.logFolder = config.LogFolder; Directory.CreateDirectory(this.logFolder); this.logfilePath = Path.Combine(this.logFolder, $"{this.filePrefix}_{DateTime.Today:yyyy-MM-dd}.log.txt"); }
/// <summary> /// (Re-)initializes this instance. /// </summary> /// <param name="config">The configuration.</param> private void Initialize(LoggingConfig config) { // initialize, but don't start. Wait for the first message to arrive. this.flushTimer = new System.Threading.Timer(_ => this.Flusher()); this.emptyFlushCount = 0; this.stoppedFlushing = true; // if re-initialized, flush old writer this.logWriter?.Flush(); this.logWriter = new LogWriter(config); Log(LogLevel.Information, nameof(Logger), "App startup"); System.Threading.Tasks.Task.Run(() => this.Cleanup()); }