コード例 #1
0
        /// <summary>
        /// このクラスの新しいインスタンスを生成します。
        /// </summary>
        public App()
        {
            this.Logger = new CompositeLogger(
                new DebugLogger(),
                new NLogger()
            {
                PublisherType        = typeof(ILoggerFacadeExtension),
                ConfigurationFactory = () =>
                {
                    var headerText = new StringBuilder();
                    headerText.AppendLine($"# {this.ProductInfo.Product} ${{var:CTG}} Log");
                    headerText.AppendLine($"# {Environment.OSVersion} - CLR {Environment.Version}");
                    headerText.AppendLine("# ${environment:PROCESSOR_ARCHITECTURE} - ${environment:PROCESSOR_IDENTIFIER}");
                    headerText.AppendLine("# ${environment:COMPUTERNAME}");
                    headerText.Append("##");

                    var header       = new NLog.Layouts.CsvLayout();
                    header.Delimiter = NLog.Layouts.CsvColumnDelimiterMode.Tab;
                    header.Quoting   = NLog.Layouts.CsvQuotingMode.Nothing;
                    header.Columns.Add(new NLog.Layouts.CsvColumn(string.Empty, headerText.ToString()));

                    var layout       = new NLog.Layouts.CsvLayout();
                    layout.Delimiter = NLog.Layouts.CsvColumnDelimiterMode.Tab;
                    layout.Quoting   = NLog.Layouts.CsvQuotingMode.Nothing;
                    layout.Header    = header;
                    layout.Columns.Add(new NLog.Layouts.CsvColumn(string.Empty, "${longdate}"));
                    layout.Columns.Add(new NLog.Layouts.CsvColumn(string.Empty, "${environment-user}"));
                    layout.Columns.Add(new NLog.Layouts.CsvColumn(string.Empty, $"{this.ProductInfo.Version}"));
                    layout.Columns.Add(new NLog.Layouts.CsvColumn(string.Empty, "${processid}"));
                    layout.Columns.Add(new NLog.Layouts.CsvColumn(string.Empty, "${threadid}"));
                    layout.Columns.Add(new NLog.Layouts.CsvColumn(string.Empty, "${message}"));

                    var file              = new NLog.Targets.FileTarget();
                    file.Encoding         = Encoding.UTF8;
                    file.Footer           = "${newline}";
                    file.FileName         = "${var:DIR}/${var:CTG}.log";
                    file.ArchiveFileName  = "${var:DIR}/archive/{#}.${var:CTG}.log";
                    file.ArchiveEvery     = NLog.Targets.FileArchivePeriod.Day;
                    file.ArchiveNumbering = NLog.Targets.ArchiveNumberingMode.Date;
                    file.MaxArchiveFiles  = 10;
                    file.Layout           = layout;

                    var memory    = new NLog.Targets.MemoryTarget();
                    memory.Layout = layout;

                    var config = new NLog.Config.LoggingConfiguration();
                    config.AddTarget(nameof(file), file);
                    config.AddTarget(nameof(memory), memory);
                    config.LoggingRules.Add(new NLog.Config.LoggingRule("*", NLog.LogLevel.Trace, file));
                    config.LoggingRules.Add(new NLog.Config.LoggingRule("*", NLog.LogLevel.Trace, memory));
                    config.Variables.Add("DIR", this.SharedDataStore.LogDirectoryPath);
                    return(config);
                },
                CreateLoggerHook = (logger, category) =>
                {
                    // ログの種類ごとにファイルを切り替える
                    logger.Factory.Configuration.Variables.Add("CTG", category.ToString());
                    logger.Factory.ReconfigExistingLoggers();
                },
            });
            this.ProductInfo     = new ProductInfo();
            this.SharedDataStore = new(this.Logger, this.ProductInfo, Process.GetCurrentProcess());
            UnhandledExceptionObserver.Observe(this, this.Logger, this.ProductInfo);
        }
コード例 #2
0
 /// <summary>
 /// このクラスの新しいインスタンスを生成します。
 /// </summary>
 public App()
 {
     UnhandledExceptionObserver.Observe(this);
     Logger.MinLogLevel          = AppConfig.MinLogLevel;
     Logger.ConfigurationFactory = () => Logger.CreateCommonConfig();
 }