C# 로그 프레임 추가

11055 단어 C#
C# 로깅은 주로 Log4Net 및 Microsoft 자체
복잡한 자동 Log 로그자습서
일단 두 번째 방식이에요.
1. DLL 다운로드 추가 필요
  • Microsoft.Practices.EnterpriseLibrary.Logging.dll
  • Microsoft.Practices.ObjectBuilder2.dll
  • Microsoft.Practices.EnterpriseLibrary.Common.dll

  • 2.Web.config의 configuration 노드에 다음 내용 추가
      <configSections>
    
        <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    
        <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    
      </configSections>
    
    
    
      <loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
    
        <listeners>
    
          <add fileName="Log\Service.log" footer="----------------------------------------" formatter="Text Formatter" rollFileExistsBehavior="Increment" rollInterval="Day" rollSizeKB="10240" timeStampPattern="yyy-MM-dd" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" traceOutputOptions="None" filter="All" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Rolling Flat File Trace Listener" />
    
        </listeners>
    
        <formatters>
    
          <add template="  : {timestamp}   : {severity}   : {title}   : {message}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="Text Formatter" />
    
        </formatters>
    
        <categorySources>
    
          <add switchValue="All" name="Debug" />
    
          <add switchValue="All" name="General">
    
            <listeners>
    
              <add name="Rolling Flat File Trace Listener" />
    
            </listeners>
    
          </add>
    
        </categorySources>
    
        <specialSources>
    
          <allEvents switchValue="All" name="All Events" />
    
          <notProcessed switchValue="All" name="Unprocessed Category" />
    
          <errors switchValue="All" name="Logging Errors &amp; Warnings" />
    
        </specialSources>
    
      </loggingConfiguration>

    참고: configSections 노드는 loggingConfiguration 노드 위에 있어야 합니다.
    3. 호출
    using System;
    
    using System.Collections.Generic;
    
    using System.Linq;
    
    using System.Text;
    
    using System.Diagnostics;
    
    using System.Reflection;
    
    using System.Configuration;
    
    using Microsoft.Practices.EnterpriseLibrary.Logging;
    
    using System.Collections;
    
    
    
    public sealed class LogWriter
    
    {
    
        public LogWriter()
    
        {
    
        }
    
    
    
        /// <summary>
    
        ///       
    
        /// </summary>
    
        /// <param name="exception"></param>
    
        public static void WriteError(Exception exception, string title)
    
        {
    
            LogEntry logEntity = new LogEntry();
    
            logEntity.Title = title;
    
            logEntity.TimeStamp = DateTime.Now;
    
            logEntity.Message = exception.ToString();
    
            logEntity.Severity = TraceEventType.Error;
    
            Logger.Write(logEntity);
    
        }
    
    }

    좋은 웹페이지 즐겨찾기