3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Serilogの設定ファイル(デスクトップアプリケーション向け)JSON版

Last updated at Posted at 2021-03-05

はじめに

Serilogの設定ファイルを備忘のため残します。
デスクトップアプリケーション向けの設定内容です。

ログレベル別に2種類(全レベル/Warning以上)× 出力形式別に2種類(通常のテキスト形式/JSON形式)
で合計4ファイルを出力する内容となっています。
各設定項目の意味は、コメントとして記載しています。

Nugetパッケージ

インストールしたSerilog関連のNugetパッケージは以下の通りです。

  • Serilog
  • Serilog.Enrichers.AssemblyName
  • Serilog.Enrichers.Environment
  • Serilog.Enrichers.Memory
  • Serilog.Enrichers.Process
  • Serilog.Enrichers.Thread
  • Serilog.Exceptions
  • Serilog.Filters.Expressions
    • 非推奨のパッケージ。.NET Core 以上であれば Serilog.Expressions
  • Serilog.Formatting.Compact
  • Serilog.Settings.Configuration
  • Serilog.Sinks.File

設定ファイル(.json)

{
  "Serilog": {
    "Using": [ "Serilog.Sinks.File" ], //ファイルに出力
    "MinimumLevel": {
      //最小ログレベル
      "Default": "Verbose"
    },
    "Enrich": [
      //拡張
      "WithThreadId", //スレッドID
      "WithThreadName", //スレッド名
      "WithMachineName", //マシン名
      "WithEnvironmentUserName", //ユーザー名
      "WithProcessId", //プロセスID
      "WithProcessName", //プロセス名
      "WithAssemblyName", //アセンブリ名
      "WithAssemblyVersion", //アセンブリバージョン
      "WithMemoryUsage", //メモリ使用量
      "WithExceptionDetails" //例外の詳細情報
    ],
    "WriteTo": [
      {
        //通常のテキスト形式で、全レベルのログを出力するためのLogger
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  //ログファイルパス
                  "path": "Logs/All/Default/all.log",
                  //ログファイルのフォーマット
                  "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss} | [{Level:u3}] | {ThreadId:00}:{ThreadName} | {ProcessId:00}:{ProcessName} | {Message:lj} | {AssemblyName} | {AssemblyVersion} | {MachineName} | {EnvironmentUserName} | {MemoryUsage} B | {NewLine}{Exception}",
                  //1日毎にロールする
                  "rollingInterval": "Day",
                  //直近の7ファイルを保持する(デフォルトでは直近の31ファイル)
                  "retainedFileCountLimit": 7,
                  //ファイルサイズの制限を削除(デフォルトでは1GB)
                  "fileSizeLimitBytes": null,
                  //書き込みをバッファリングする
                  "buffered": true
                }
              }
            ]
          }
        }
      },
      {
        //JSON形式で、全レベルの構造化ログを出力するためのLogger
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "path": "Logs/All/Compact/all_compact.json",
                  //JsonFormatter
                  "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact",
                  "rollingInterval": "Day",
                  "retainedFileCountLimit": 7,
                  "fileSizeLimitBytes": null,
                  "buffered": true
                }
              }
            ]
          }
        }
      },
      {
        //通常のテキスト形式で、Warning以上のログを出力するためのLogger
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "Filter": [
              {
                "Name": "ByIncludingOnly",
                "Args": {
                  "expression": "(@Level = 'Error' or @Level = 'Fatal' or @Level = 'Warning')"
                }
              }
            ],
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "path": "Logs/Error/Default/error.log",
                  "outputTemplate": "{Timestamp:yyyy-MM-dd HH:mm:ss} | [{Level:u3}] | {ThreadId:00}:{ThreadName} | {ProcessId:00}:{ProcessName} | {Message:lj} | {AssemblyName} | {AssemblyVersion} | {MachineName} | {EnvironmentUserName} | {MemoryUsage} B | {NewLine}{Exception}",
                  "rollingInterval": "Day",
                  "retainedFileCountLimit": 7,
                  "fileSizeLimitBytes": null,
                  "buffered": true
                }
              }
            ]
          }
        }
      },
      {
        //JSON形式で、Warning以上の構造化ログを出力するためのLogger
        "Name": "Logger",
        "Args": {
          "configureLogger": {
            "Filter": [
              {
                "Name": "ByIncludingOnly",
                "Args": {
                  "expression": "(@Level = 'Error' or @Level = 'Fatal' or @Level = 'Warning')"
                }
              }
            ],
            "WriteTo": [
              {
                "Name": "File",
                "Args": {
                  "path": "Logs/Error/Compact/error_compact.json",
                  "formatter": "Serilog.Formatting.Compact.CompactJsonFormatter, Serilog.Formatting.Compact",
                  "rollingInterval": "Day",
                  "retainedFileCountLimit": 7,
                  "fileSizeLimitBytes": null,
                  "buffered": true
                }
              }
            ]
          }
        }
      }
    ]
  }
}
3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?