예제 #1
0
        private ILogger SetupLogging()
        {
            const string fileOutputTemplate = "{Timestamp:dd-MM-yyyy HH:mm:ss.fff} [{Level}] {Message:lj}{NewLine}{Exception}";
            string       basePath           = MiscellaneousUtils.GetLogsPath();
            var          logs = new Dictionary <string, string>
            {
                { typeof(NavPageViewModel).Namespace, "vm_.log" },
                { $"{typeof(SyncService).FullName}", "sync_service_.log" },
                { $"{typeof(TaskListDataService).FullName}", "data_tasklist_service_.log" },
                { $"{typeof(TaskDataService).FullName}", "data_task_service_.log" },
                { $"{typeof(UserDataService).FullName}", "data_user_service_.log" },
                { $"{typeof(MiraiNotesDataService).FullName}", "data_main_service_.log" },
                { $"{typeof(SyncBackgroundTask).FullName}", "bg_sync_.log" },
                { $"{typeof(MarkAsCompletedBackgroundTask).FullName}", "bg_marktaskascompleted_.log" },
                { $"{typeof(AuthenticatedHttpClientHandler).FullName}", "auth_http_handler_.txt" }
            };

            var loggerConfig = new LoggerConfiguration()
                               .MinimumLevel.Verbose();

            foreach (var kvp in logs)
            {
                loggerConfig.WriteTo.Logger(l => l
                                            .Filter.ByIncludingOnly(Matching.FromSource(kvp.Key))
                                            .WriteTo.File(
                                                Path.Combine(basePath, kvp.Value),
                                                rollingInterval: RollingInterval.Day,
                                                rollOnFileSizeLimit: true,
                                                outputTemplate: fileOutputTemplate,
                                                shared: true));
            }

            Log.Logger = loggerConfig.CreateLogger();
            return(Log.Logger);
        }
예제 #2
0
 private void DeleteOldLogs()
 {
     try
     {
         _logger.Information($"{nameof(DeleteOldLogs)}: Deleting old log files...");
         FileUtils.DeleteFilesInDirectory(MiscellaneousUtils.GetLogsPath(), DateTime.Now.AddDays(-3));
     }
     catch (Exception e)
     {
         _logger.Error(e, $"{nameof(DeleteOldLogs)}: Unknown error while deleting old files..");
         _telemetryService.TrackError(e);
     }
 }