クラス



ログ出力をもらう

  1. 使う側
// 必要に応じて、フォームに出力する
// 例はコンソールに出力しているが、コンソール出力ならlog4netのConsoleAppenderに
// 出力すればよい。
Foo.Printing += (se, ea) => Console.Write("[FOO]" + ea.Message);
  1. クラス定義側
/// <summary>log4netのアペンダー:イベントを通じてメッセージを外に送る</summary>
public class Foo
{
    /// <summary>イベントの定義</summary>
    public static event PringEventHandler Printing = (p1, p2) => { };

    /// <summary>FooApenderから見れるようにする</summary>
    internal static PringEventHandler _Printing
    {
        get
        {
            return Printing;
        }
    }

    /// <summary>イベント型</summary>
    public delegate void PringEventHandler(object sender, PrintEventArgs ea);

    /// <summary>イベント引数</summary>
    public class PrintEventArgs : EventArgs
    {
        /// <summary>メッセージ</summary>
        public string Message { get; set; }
        /// <summary>コンストラクタ</summary>
        public PrintEventArgs(string message)
        {
            this.Message = message;
        }
    }
}
public class FooApender : log4net.Appender.AppenderSkeleton
{
    protected override void Append(log4net.Core.LoggingEvent loggingEvent)
    {
        string s = RenderLoggingEvent(loggingEvent);
        Foo._Printing(this, new Foo.PrintEventArgs(s));
    }
}
最終更新:2009年08月24日 12:16