0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【C#】手っ取り早くNLogを導入する(3分)

Posted at

超ざっくり手順。
初心者の方でこの記事にたどり着いてしまった方はすみません、戻って別の記事を読んでください。

手順

(1)NugetでNLogをインストールする。
(2)NLog.configを追加する
(3)NLog.configに次の内容を全コピペする。

NLog.config
<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      autoReload="true"
      throwExceptions="true"
      internalLogLevel="Off" internalLogFile="./logs/nlog_internal.log">

	<targets>
		<!-- ファイル -->
		<target name="logFile"
                xsi:type="File"
                encoding="UTF-8"
                writeBom="true"
                lineEnding="Default"
                layout="${longdate} ${level:uppercase=true:padding=-5} [${threadid}] ${logger} - ${message} ${exception:format=tostring}"
                fileName="./logs/${processname}.log"
                archiveFileName="./logs/backup/${processname}_{###}.log"
                archiveEvery="Day"
                archiveNumbering="Sequence"
                maxArchiveFiles="20" />
	</targets>
	<rules>
		<logger name="*" minlevel="Trace" writeTo="logFile" />
	</rules>
</nlog>

(4)NLogを使いたいクラスのメンバー変数として以下を追加する。

NLogUsed.cs
static NLog.Logger _logger = NLog.LogManager.GetCurrentClassLogger();

(5)上記の_loggerの適当なメソッドを呼べばログを吐く。

NLogUsed.cs
_logger.Info("やったぜ!");

忘備録

使用する他のプロジェクトでもNLogを使用したい場合、
そのプロジェクトでもNLogのインストールは必要だが、NLog.configの作成はしなくてよい。
NLog.configはエントリーポイントがあるプロジェクトで、設定値が1度だけ読み込まれる。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?