複数のプロセスのログファイルを避けるために出現ログをトリガしたり、お互いの不足している質問を上書きし、同じファイルのロックに書き込まれ、前の使用バッファがMSMQでこれを解決します。
2年以上前教育に参加すると、国内の航空会社系変換プロジェクトを訓練し、MSMQそれを使用する建築家の計画を記録します。
各APは、彼のMSMQを書き込み、あなたができるように、その後、たまに中を収集するために、ログサーバを別のログます中央集中型のログ管理、しかし、また排除通信の一時的な内訳質問。
1.最初のWindows MSMQとMSMQ専用キューの確立を有効にするには、次の2つを参照することができます:
[PowerShellの]を有効にWindows ServerのMSMQ機能(メッセージキュー)
【のPowershell] MSMQ(メッセージキュー)のセットを確立します
我々はプライベートキューlogqueueのための上記の名前に基づいて確立していると仮定
サンプルコードは、MSMQを使用することができる有する2.log4net
Visual Studioのプロジェクトを開く前に、クラスを追加します。 MsmqAppender.cs
#region Apache License
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion
using System;
using System.Messaging;
using log4net.Core;
namespace Appender
{
///
/// Appender writes to a Microsoft Message Queue
///
///
/// This appender sends log events via a specified MSMQ queue.
/// The queue specified in the QueueName (e.g. .Private$log-test) must already exist on
/// the source machine.
/// The message label and body are rendered using separate layouts.
///
public class MsmqAppender : log4net.Appender.AppenderSkeleton
{
private MessageQueue m_queue;
private string m_queueName;
private log4net.Layout.PatternLayout m_labelLayout;
public MsmqAppender()
{
}
public string QueueName
{
get { return m_queueName; }
set { m_queueName = value; }
}
public log4net.Layout.PatternLayout LabelLayout
{
get { return m_labelLayout; }
set { m_labelLayout = value; }
}
override protected void Append(LoggingEvent loggingEvent)
{
if (m_queue == null)
{
if (MessageQueue.Exists(m_queueName))
{
m_queue = new MessageQueue(m_queueName);
}
else
{
ErrorHandler.Error("Queue ["+m_queueName+"] not found");
}
}
if (m_queue != null)
{
Message message = new Message();
message.Label = RenderLabel(loggingEvent);
using(System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
System.IO.StreamWriter writer = new System.IO.StreamWriter(stream, new System.Text.UTF8Encoding(false, true));
base.RenderLoggingEvent(writer, loggingEvent);
writer.Flush();
stream.Position = 0;
message.BodyStream = stream;
m_queue.Send(message);
}
}
}
private string RenderLabel(LoggingEvent loggingEvent)
{
if (m_labelLayout == null)
{
return null;
}
System.IO.StringWriter writer = new System.IO.StringWriter();
m_labelLayout.Format(writer, loggingEvent);
return writer.ToString();
}
}
}
3.次に設定log4net.config
4.テストプログラム:1,000書き込みログを実行します
private static readonly ILog Logger = LogManager.GetLogger(typeof(testLog4net));
[TestMethod]
public void TestMethod1()
{
XmlConfigurator.Configure(new FileInfo(string.Format(@"{0}{1}",
Directory.GetParent(Directory.GetParent(Environment.CurrentDirectory).FullName),
"log4net.config")));
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 1000; i++)
{
Logger.Debug(string.Format("this is no{0} log", i + 1));
}
sw.Stop();
Console.WriteLine("总耗时(ElapsedMilliseconds) = " + sw.ElapsedMilliseconds);
}
テスト結果:1,000ペンログ1.721ミャオ族、スピード!
本当にそこプライベートqueueslogqueue千のペンログ・キュー
要約:
- これはlog4netのログはXML形式で記述され、その後、別の時間ログエントリのJSON形式に書き換えられ、MQViewerから発見されました。
- これは、ログにノートがMSMQ書かれ、その後、次のノートは、MSMQから読み込まれ、その後、ログファイルに書き込まれています。
参考:
サンプルアペンダ
log4netの
MSMQサンプルコード
原文:大专栏 [.NET][Log4net]解决FileAppender+MinimalLock性能问题(二)(MSMQ)