static Logger() { var loggerConfiguration = new LoggerConfiguration() .Filter.ByExcluding(logEvent => logEvent.Exception != null && (logEvent.Exception.GetType() == typeof(OperationCanceledException) || logEvent.Exception.GetType() == typeof(ObjectDisposedException))) // UI Logger .WriteTo.Sink(m_UserInterfaceSink); // File Logger var entryName = Assembly.GetEntryAssembly()?.GetName().Name ?? string.Empty; if (string.IsNullOrEmpty(entryName)) { entryName = Assembly.GetExecutingAssembly()?.GetName().Name ?? string.Empty; } if (!string.IsNullOrEmpty(entryName)) { var folder = Environment.ExpandEnvironmentVariables($"%LocalAppData%\\{entryName}\\"); var addTextLog = FileSystemUtils.DirectoryExists(folder); if (!addTextLog) { try { FileSystemUtils.CreateDirectory(folder); addTextLog = true; } catch { // ignored } } if (addTextLog) { loggerConfiguration = loggerConfiguration // Exceptions .WriteTo.Logger(lc => lc.Filter.ByIncludingOnly(le => le.Exception != null).WriteTo.File( folder + "ExceptionLog.txt", rollingInterval: RollingInterval.Month, retainedFileCountLimit: 3, outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff}\t{Level}\t\"{Exception:l}\"{NewLine}")) //CSV .WriteTo.File(folder + "ApplicationLog.txt", rollingInterval: RollingInterval.Day, outputTemplate: "{Timestamp:HH:mm:ss}\t{Level:w3}\t{Message:l}{NewLine}") // Json .WriteTo.File(formatter: new JsonFormatter(renderMessage: true), path: folder + "ApplicationLog.json", rollingInterval: RollingInterval.Day); } } // Start logging Log.Logger = loggerConfiguration.CreateLogger(); Log.Information("Application start"); }
/// <summary> /// Writes the file ans displays performance information /// </summary> /// <param name="fileSetting">The file setting.</param> /// <param name="showSummary"> /// Flag indicating that a message Box should be displayed. If <c>true</c> a message box will be /// shown /// </param> /// <param name="cancellationToken">A CancellationToken</param> /// <param name="fileSettingSourcesCurrent">The file setting sources current.</param> /// <param name="settingLaterThanSources">The setting later than sources.</param> /// <param name="ask">if set to <c>true</c> [ask].</param> /// <returns> /// Number of record written to file /// </returns> public static long WriteFileWithInfo(this IFileSetting fileSetting, bool showSummary, FileSettingChecker fileSettingSourcesCurrent, bool ask, bool onlyOlder, CancellationToken cancellationToken) { if (fileSetting == null) { return(0); } long written = 0; var fi = FileSystemUtils.FileInfo(fileSetting.FullPath); var dir = FileSystemUtils.GetDirectoryName(fi.FullName); if (!FileSystemUtils.DirectoryExists(dir)) { if (_MessageBox.Show(null, $"The directory {dir.RemovePrefix()} does not exist, should it be created?", "Directory", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { FileSystemUtils.CreateDirectory(dir); } else { return(0); } } var fileInfo = new FileInfo(fileSetting.FullPath); if (fileInfo.Exists) { fileSetting.FileLastWriteTimeUtc = fi.LastWriteTimeUtc; } var stringBuilder = new System.Text.StringBuilder(); using (var processDisplay = fileSetting.GetProcessDisplay(null, true, cancellationToken)) { fileSettingSourcesCurrent?.Invoke(fileSetting, processDisplay); if (onlyOlder && fileSetting.SettingLaterThanSources(processDisplay.CancellationToken)) { return(0); } fileSetting.FullPath.DeleteFileQuestion(ask); var errors = new RowErrorCollection(50); var writer = fileSetting.GetFileWriter(processDisplay.CancellationToken); writer.ProcessDisplay = processDisplay; writer.Warning += errors.Add; written = writer.Write(); var hasIssues = !string.IsNullOrEmpty(writer.ErrorMessage) || (errors.CountRows > 0); if (showSummary || hasIssues) { fi = FileSystemUtils.FileInfo(fileSetting.FullPath); // if all source settings are file settings, get the latest file time and set this fileTime var latest = DateTime.MinValue; fileSetting.GetSourceFileSettings(delegate(IFileSetting setting) { if (!(setting is IFileSettingRemoteDownload)) { if (latest < setting.FileLastWriteTimeUtc) { latest = setting.FileLastWriteTimeUtc; } } else { var fiSrc = FileSystemUtils.FileInfo(setting.FullPath); if (fiSrc.Exists && latest < fiSrc.LastWriteTimeUtc) { latest = fiSrc.LastWriteTimeUtc; } } return(false); }, cancellationToken); stringBuilder.Append($"Finished writing file\r\rRecords: {written:N0}\rFile size: {fi.Length / 1048576.0:N} MB"); if (latest < DateTime.MaxValue && latest > DateTime.MinValue) { stringBuilder.Append($"\rTime adjusted to latest source file: {latest.ToLocalTime():D}"); fi.LastWriteTimeUtc = latest; fileSetting.FileLastWriteTimeUtc = latest; } if (hasIssues) { stringBuilder.Append("\rIssues:\r"); } if (!string.IsNullOrEmpty(writer.ErrorMessage)) { stringBuilder.Append(writer.ErrorMessage); } if (errors.CountRows > 0) { stringBuilder.Append(errors.DisplayByRecordNumber); } } } if (stringBuilder.Length > 0) { _MessageBox.Show(null, stringBuilder.ToString(), FileSystemUtils.GetShortDisplayFileName(fileSetting.FileName, 80), MessageBoxButtons.OK); } return(written); }