私は多くのスレッド(〜50)を実行するアプリケーションを持っています。すべてのスレッドは、データベースから処理するロックを持つ支払いを取得し、処理します。
しかし、50個のスレッドは、ログを読めなくし、混乱させ、膨大なファイルサイズにします。
ここでは、すべての支払いIDに対して個別のファイルにNLog書き込みを行い、支払い処理に関するすべての履歴を1つのファイルに保存するようにします。NLogをスレッドセーフな方法で実行時に別のファイルターゲットに書き込む方法を教えてください。
NLog設定ファイル:今
<?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">
<targets>
<target name="file" xsi:type="AsyncWrapper" queueLimit="10000" overflowAction="Discard">
<target name="f" xsi:type="File" fileName="C:\LogFiles\Immediate\${shortdate}.server.log" layout="${longdate}|${level}|${processid}|${threadid}|${level:upperCase=true}|${callsite:className=false}|${message}" encoding="utf-8"/>
</target>
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file"/>
</rules>
</nlog>
マイコード:
private static readonly ILogger Logger = LogManager.GetCurrentClassLogger(); // "2017-07-20.log"
while (!_cancellationToken.IsCancellationRequested)
{
using (var session = _connectionFactory.GetSession())
{
AcquirePaymentWithUpdlockReadpast(session,
Logger, // "2017-07-20.log"
payment => {
if (payment == null)
return;
Logger.Debug($"Payment {payment.Id} has been acquired for processment");
ProcessPayment(session, Logger, payment); // "2017-07-20.log"
});
}
Thread.Sleep(50);
}
私はそれがあることを期待するもの:私はLogManager.Configuration
を使用するいくつかの解決策を見つけた
private static readonly ILogger GeneralLogger = LogManager.GetCurrentClassLogger(); // "2017-07-20.General.log"
while (!_cancellationToken.IsCancellationRequested)
{
using (var session = _connectionFactory.GetSession())
{
AcquirePaymentWithUpdlockReadpast(session,
GeneralLogger, // "2017-07-20.General.log"
payment => {
if (payment == null)
return;
var paymentLogger = LogManager.GetCurrentClassLogger();
paymentLogger.FileName = $"Payment-{payment.Id}.log"; // <-- I want this.
paymentLogger.Debug($"Payment has been acquired for processment");
ProcessPayment(session, paymentLogger, payment); // "2017-07-20.Payment-123.log"
});
}
Thread.Sleep(50);
}
は、しかし、スレッドセーフではないようです。
ありがとうございました。 2番目のアプローチは私を助けました、私はこの方法でロガー名を使用することはできませんでした。しかし、私は 'name'プロパティを持つ少し短い解決法を見つけました。それはあなたにとって興味深いかもしれません:) –