6
9

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 5 years have passed since last update.

log4netでログにプロセス番号を出力する

Posted at

log4netにはデフォルトでプロセス番号をログに出すようなパターン名が無い。
log4netのプロパティを用いて出力することとなる。

まず、ログを初めて出力する行より前の領域にて、log4netのプロパティにプロセス番号を設定する。
ここでは、pidをキーとしてプロセス番号を設定する。

    static void Main()
    {
      // プロセス番号をlog4netのプロパティに設定する。
      // このプロパティをログ出力設定ファイルにて指定する。
      log4net.GlobalContext.Properties["pid"] = System.Diagnostics.Process.GetCurrentProcess().Id;

      logger.Info("Program starts");

ログ出力設定ファイルにて、パターン名%property(%Pも可)にて、上で設定したキーを指定する。

log4net.xml(抜粋)
<ConversionPattern value="%date [%property{pid}] [%-5level] (%method) - %message%n" />
出力例
2015-03-20 19:24:01,990 [5288] [INFO ] (Main) - Program starts
  • 5288がプロセス番号
6
9
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
6
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?