[.NET][C#]Log日誌功能(一)Microsoft.Practices.EnterpriseLibrary.Logging

Log是電腦上發生動作和事件的歷史,紀錄時若有統一的方法作為標準,對事後問題的追查及分析將會有很大的幫助,

在.NET環境常見幾種元件可以輔助我們完成這項工作,幾年前厲害的同事幫我們選擇了log4net,實際上也很方便,最近想調整小地方,剛好來試其他幾種。

  • Microsoft Enterprise Library
  • NLog
  • NSpring
  • ELMAH

 

Enterprise Library中的Logging Application Block 6.0版本之前5.0版本前在初始logWriter instance方式有點不同,筆記基本的引用方式。

範例會用預設的Windows Event log以及flat file露出。

 

Enterprise版本:

Enterprise Library

1.首先新增一個Console專案,並從Nuget分別安裝 EnterpriseLibrary.CommonEnterpriseLibrary.Logging

2.使用工具EntLibConfig.exe新增一組Log設定。

預設的config設定在Event Log

3.打開Console專案內的Program.cs程式

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System;

namespace TestLogConsole
{
    class Program
    {
        static void Main(string[] args)
        {

            IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
            LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
            Logger.SetLogWriter(logWriterFactory.Create());

            Logger.Write("test", "General");
            if (Logger.IsLoggingEnabled())
            {
                LogEntry log = new LogEntry();
                log.Severity = System.Diagnostics.TraceEventType.Information;
                log.Message = "我來自EnterpriseLibrary.Logging";
                log.Title = "EnterpriseLibrary";
                log.TimeStamp = DateTime.Now;
                Logger.Write(log);

                log.Severity = System.Diagnostics.TraceEventType.Error;
                log.Message = "我來自EnterpriseLibrary.Logging";
                log.Title = "EnterpriseLibrary";
                log.TimeStamp = DateTime.Now;
                Logger.Write(log);
            }
        }
    }
}

 

4.新增Windows EventSource

為了避免因為權限沒辦法建立Event Source(Enterprise Library Logging),找到兩個解法:

  • 要記得用系統管理員身份執行Visual Studio
  • 或是請SP執行Powershell command先新增好Event Source。
$source = "Enterprise Library Logging"
if ([System.Diagnostics.EventLog]::SourceExists($source) -eq $false) 
{ 
   [System.Diagnostics.EventLog]::CreateEventSource($source, "Application")
}

Powershell執行結果

5.執行測試

6.開啟事件檢視器(Event Viewer)

Windows R + Eventvwr

果然出現3筆紀錄,其中2筆等級為資訊,1筆等級為錯誤。

事件的內容

試試看其他log寫出方式。

7.新增logging target listener,這邊我們選 Add flat File Trace Listener,設定完成後儲存。

Log categories選新增的listener,預設檔名是trace.log,也可以選擇非同步(Asynchronously)的方式寫出檔案

8. 執行測試

小結:

  • 有UI: EntLibConfig.exe設定起來很快。
  • Enterprise Library is a collection of application blocks intended for use by developers who build complex, enterprise-level applications.

 

參考:

Enterprise Library

HashTag Enterprise Library Logging Application Block Helper Library


How to: Add Your Application as a Source of Event Log Entries

Enterprise Library Logging not logging to Event Log from ASP.NET

Enterprise Library 6 LogCallHandler throwing exception “The LogWriter has not been set for the Logger static class”

Write to multiple files Enterprise Library Logger

Benchmarking 5 Popular .NET Logging Libraries