.NETコンソールプログラムでNLogを設定して、エラーと致命的なエラーをWindowsのイベントログに記録し、すべてをファイルに記録し、本当に情報、警告のみを記録しようとしています、エラー、および致命的なエラーがコンソールに表示されます。NLog Configuration ::どの出力がどの出力に移動するかを制御する
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
</startup>
<nlog autoReload="true" internalLogFile="log/internal.txt" internalLogLevel="Trace">
<variable name="brief" value="${longdate} ${level} ${logger} ${message}${onexception:inner= ${exception:format=toString,Data}} "/>
<variable name="verbose" value="${longdate} | ${machinename} | ${processid} | ${processname} | ${level} | ${logger} | ${message}${onexception:inner= ${exception:format=toString,Data}}"/>
<targets>
<target name="console" type="Console" layout="${brief}"/>
<target name="all_in" type="File" layout="${verbose}" fileName="${basedir}/log/log.txt" archiveFileName="${basedir}/log/archive/log.{#}.txt" archiveNumbering="DateAndSequence" archiveAboveSize="1048576" archiveDateFormat="yyyyMMdd" keepFileOpen="false" encoding="iso-8859-2"/>
<target name="events" type="EventLog" layout="${verbose}" source="nlog-test" onOverflow="Split"/>
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="console"/>
<logger name="*" minlevel="Trace" writeTo="all_in"/>
<logger name="*" minlevel="Fatal" maxlevel="Error" writeTo="events"/>
</rules>
</nlog>
</configuration>
私は単純なコンソールプログラムを書いて、これをテストしてユーザーからのコマンドを読み取り、適切なレベルに書き込みます。ご覧のとおり、内部ログ機能を有効にして、NLogがやっていることとその理由を理解できるようにしました。
これは、イベントログにエラー(良い)と情報(悪い)を書き出し始めました。今はイベントログに何も書き込んでいません。 "maxlevel"がErrorとして定義されているときに、なぜ "Info"レベルをイベントログに書き込んでいるのか分かりません。また、 "Info"は "higher"です。それはイベントログには何も書いて停止した理由私はまた、その後maxlevel="Error"
を削除し、minlevel="Error"
にminlevel="Fatal"
を変更.. events
-logging-ルールの
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using NLog;
namespace nlog_test
{
class Program
{
static Logger logger = LogManager.GetCurrentClassLogger();
static void Main(string[] args)
{
while(true)
{
try
{
Console.Write("nlog-test> ");
var operands = Console.ReadLine().Split(
new[] {' ', '\t'},
StringSplitOptions.RemoveEmptyEntries);
if(operands.Length == 0)
{
continue;
}
var command = operands[0];
var arguments = operands.Skip(1);
Action<Action<string>> log =
f => f(string.Join(" ", arguments));
switch(command)
{
case "debug":
log(logger.Debug);
break;
case "delete":
File.Delete("log/log.txt");
break;
case "error":
log(logger.Error);
break;
case "fatal":
log(logger.Fatal);
break;
case "help":
Console.Write(@"
COMMAND [ARG...]
Commands:
debug TEXT...
Delete the log file.
delete
Delete the log file.
error TEXT...
Write an error message.
fatal TEXT...
Write a fatal error message.
help
Print this message.
info TEXT...
Write an information message.
print
Print the contents of the log file.
quit
Exit the process.
trace TEXT...
Write a trace message.
warn TEXT...
Write a warning message.
");
break;
case "info":
log(logger.Info);
break;
case "print":
{
var psi = new ProcessStartInfo("less.exe", "log/log.txt");
//psi.CreateNoWindow = true;
psi.UseShellExecute = false;
using(var process = Process.Start(psi))
{
process.WaitForExit();
}
}
break;
case "quit":
return;
case "trace":
log(logger.Trace);
break;
case "warn":
log(logger.Warn);
break;
default:
Console.Error.WriteLine(
"Unknown command: {0}",
command);
break;
}
}
catch(Exception ex)
{
Console.Error.Write($"Caught unhandled exception: ");
Console.Error.WriteLine(ex);
}
}
}
}
}
events-logging-ruleの場合、maxlevel = "Error"を削除し、minlevel = "Fatal"をminlevel = "Error"に変更します。 –
@RolfKristensenありがとう。私はそれが問題だと思う。もともと私はminlevelを持っていましたが、なんらかの理由でそれが誤っていると思って、それが後方にあると思っていました...私は、それが正しいと思いました。これは、私のテストプログラムがなぜイベントビューアへのロギングを停止したのかをも説明しています。もともとはmax = Errorだったので、min = Fatalを追加しました。 min =致命的かつmax = Errorはマッチを生成しないので、それ以降はログに記録されません。私のアカウントはあなたのコメントをあきらめるのに十分な大きさではありませんが、回答を投稿したい場合はそれを受け入れることができます。 –