Logger(log4net)参考

Logger参考

<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net " />
  </configSections>

  <!-- log4net設定 -->
  <log4net>
    <appender name="ApiRollingLogFileAppender" type="log4net.Appender.RollingFileAppender,log4net">
      <param name="File" value="./log/app" />
      <param name="AppendToFile" value="true" />
      <param name="RollingStyle" value="Composite" />
      <param name="MaxSizeRollBackups" value="100" />
      <param name="MaximumFileSize" value="1GB" />
      <param name="StaticLogFileName" value="false" />
      <param name="DatePattern" value="&quot;_&quot;yyyyMMdd&quot;.log&quot;" />
      <layout type="log4net.Layout.PatternLayout,log4net">
        <param name="ConversionPattern" value="%d [%5p] - %m%n" />
      </layout>
      <Encoding value="UTF-8" />
    </appender>
    <appender name="VsTraceAppender" type="xxxx.app.common.util.VsTraceAppender">
     <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date [%thread] %-5level %logger [%ndc] - %message%newline" />
     </layout>
    </appender>    
    <root>
      <level value="ALL" />      
      <appender-ref ref="ApiRollingLogFileAppender" />
      <appender-ref ref="VsTraceAppender" />
    </root>
  </log4net>
    :
    :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Web;
using log4net;
using log4net.Core;
using log4net.Appender;

namespace mvapi.app.common.util
{
    /// <summary>
    /// ロガークラス
    /// </summary>
    public static class logger
    {
        /// <summary>
        /// ロガーインターフェース
        /// </summary>
        private static ILog mylogger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// メッセージフォーマット
        /// </summary>
        private const string MSG_FORMAT = "{0},{1},{2},{3}";
        
        /// <summary>
        /// システムを停止するような致命的なエラー
        /// </summary>
        public static void Fatal(string msg, [CallerFilePath] string callerFilePath = "", [CallerMemberName] string callerMemberName = "", [CallerLineNumber] int callerLineNumber = 0)
        {
            mylogger.Fatal(string.Format(MSG_FORMAT, callerFilePath, callerMemberName, callerLineNumber, msg));
        }

        /// <summary>
        /// システム停止までいかないが、問題となるエラー
        /// </summary>
        public static void Error(string msg, [CallerFilePath] string callerFilePath = "", [CallerMemberName] string callerMemberName = "", [CallerLineNumber] int callerLineNumber = 0)
        {
            mylogger.Error(string.Format(MSG_FORMAT, callerFilePath, callerMemberName, callerLineNumber, msg));
        }

        /// <summary>
        /// 注意や警告
        /// </summary>
        public static void Warn(string msg, [CallerFilePath] string callerFilePath = "", [CallerMemberName] string callerMemberName = "", [CallerLineNumber] int callerLineNumber = 0)
        {
            mylogger.Warn(string.Format(MSG_FORMAT, callerFilePath, callerMemberName, callerLineNumber, msg));
        }

        /// <summary>
        /// 操作ログ情報
        /// </summary>
        public static void Info(string msg, [CallerFilePath] string callerFilePath = "",[CallerMemberName] string callerMemberName = "",[CallerLineNumber] int callerLineNumber = 0)
        {
            mylogger.Info(string.Format(MSG_FORMAT, callerFilePath, callerMemberName, callerLineNumber, msg));
        }

        /// <summary>
        /// 開発用デバッグ情報
        /// </summary>
        public static void Debug(string msg, [CallerFilePath] string callerFilePath = "", [CallerMemberName] string callerMemberName = "", [CallerLineNumber] int callerLineNumber = 0)
        {
            mylogger.Debug(string.Format(MSG_FORMAT, callerFilePath, callerMemberName, callerLineNumber, msg));
        }
    }
}