Skip to content

Instantly share code, notes, and snippets.

@nissuk
Created February 16, 2014 12:47
Show Gist options
  • Save nissuk/9033726 to your computer and use it in GitHub Desktop.
Save nissuk/9033726 to your computer and use it in GitHub Desktop.
C#: log4net RollingFileAppenderの継承
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<log4net>
<!-- 作成したAppenderの型名をtypeで指定 -->
<appender name="Custom" type="example.CustomRollingFileAppender">
<!-- RollingFileAppender等の既存のパラメータ -->
<param name="File" value="log\LOG_" />
<param name="DatePattern" value='yyyyMMdd".log"' />
<param name="StaticLogFileName" value="false" />
<param name="AppendToFile" value="true" />
<param name="MaxSizeRollBackups" value="10" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-5level %logger [%property{NDC}] - %message%newline" />
</layout>
<!-- 新しく定義したパラメータ -->
<param name="Test" value="Test Value" />
</appender>
<root>
<level value="DEBUG" />
<!-- 使用する Appender -->
<appender-ref ref="Custom" />
</root>
</log4net>
</configuration>
using log4net.Appender;
using log4net.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Example
{
/// <summary>
/// RollingFileAppenderを継承したカスタムのAppenderです。
/// (拡張する箇所の説明用。メソッドのXMLコメントはRollingFileAppenderのものです)
/// </summary>
public class CustomRollingFileAppender : RollingFileAppender
{
/// <summary>
/// App.config等設定ファイルでparamに定義した設定はプロパティで受け取ることができます。
/// &lt;param name="Test" value="Test Value" /&gt;等と設定すると下記プロパティに値がセットされます。
/// </summary>
public string Test { get; set; }
/// <summary>
/// Initialize the appender based on the options set
/// </summary>
/// <remarks>
/// <para>
/// This is part of the <see cref="IOptionHandler"/> delayed object
/// activation scheme. The <see cref="ActivateOptions"/> method must
/// be called on this object after the configuration properties have
/// been set. Until <see cref="ActivateOptions"/> is called this
/// object is in an undefined state and must not be used.
/// </para>
/// <para>
/// If any of the configuration properties are modified then
/// <see cref="ActivateOptions"/> must be called again.
/// </para>
/// <para>
/// Sets initial conditions including date/time roll over information, first check,
/// scheduledFilename, and calls <see cref="ExistingInit"/> to initialize
/// the current number of backups.
/// </para>
/// </remarks>
public override void ActivateOptions()
{
// ActivateOptions()はAppenderの初期化時に実行されます。
// 初期化時に何かしたいときはこの辺りに処理を追加します。
// baseのRollingFileAppender#ActivateOptions()→FileAppender#ActivateOptions()が
// 実行されるまではFileパラメータがフルパスではないため、
// その前にフルパスが必要な場合は自分でフルパスにする必要があります。
// ※下記はRollingFileAppenderの一部処理と同様です。
string baseFileName = string.Empty;
if (SecurityContext == null)
{
SecurityContext = SecurityContextProvider.DefaultProvider.CreateSecurityContext(this);
}
using (SecurityContext.Impersonate(this))
{
base.File = ConvertToFullPath(base.File.Trim());
baseFileName = base.File;
}
// 何かする?
base.ActivateOptions();
}
/// <summary>
/// Write out a logging event.
/// </summary>
/// <param name="loggingEvent">the event to write to file.</param>
/// <remarks>
/// <para>
/// Handles append time behavior for RollingFileAppender. This checks
/// if a roll over either by date (checked first) or time (checked second)
/// is need and then appends to the file last.
/// </para>
/// </remarks>
protected override void Append(log4net.Core.LoggingEvent loggingEvent)
{
// Append(LoggingEvent), Append(LoggingEvent[]) はログ出力時に実行されます。
// ログ出力時に何かしたいときはこの辺りに処理を追加します。
// RollingFileAppenderの場合AdjustFileBeforeAppend()を実行してから
// base.Append()を実行する形になっているのでAdjustFileBeforeAppend()をoverrideしてもOKです。
base.Append(loggingEvent);
}
/// <summary>
/// Write out an array of logging events.
/// </summary>
/// <param name="loggingEvents">the events to write to file.</param>
/// <remarks>
/// <para>
/// Handles append time behavior for RollingFileAppender. This checks
/// if a roll over either by date (checked first) or time (checked second)
/// is need and then appends to the file last.
/// </para>
/// </remarks>
protected override void Append(log4net.Core.LoggingEvent[] loggingEvents)
{
// 上記と同様
base.Append(loggingEvents);
}
/// <summary>
/// Performs any required rolling before outputting the next event
/// </summary>
/// <remarks>
/// <para>
/// Handles append time behavior for RollingFileAppender. This checks
/// if a roll over either by date (checked first) or time (checked second)
/// is need and then appends to the file last.
/// </para>
/// </remarks>
protected override void AdjustFileBeforeAppend()
{
base.AdjustFileBeforeAppend();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment