我在使用6.0版本時也遇到同樣的問題。 我通過創建自己的靜態記錄器類並在構造函數中初始化SetLogWriter()來解決此問題。下面是代碼:
using System;
using ms = Microsoft.Practices.EnterpriseLibrary.Logging;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
namespace LoggingService
{
public static class Logger
{
static Logger()
{
try
{
IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
ms.LogWriterFactory logWriterFactory = new ms.LogWriterFactory(configurationSource);
ms.Logger.SetLogWriter(new ms.LogWriterFactory().Create());
}
catch (Exception)
{
}
}
/// <summary>
/// Writes exception details to the log using Enterprise Library
/// </summary>
/// <param name="ex"></param>
public static void Log(Exception ex)
{
// can be changed as per your requirement for the event/priority and the category
ms.Logger.Write(ex, "ErrorsWarnings", 1, 1, System.Diagnostics.TraceEventType.Error);
}
/// <summary>
/// Writes Information message to the log using Enterprise Library
/// </summary>
/// <param name="infoMsg"></param>
public static void Log(string infoMsg)
{
ms.Logger.Write(infoMsg);
}
}
}
,簡單地使用像
Logger.Log(ex);
//or
Logger.Log("Your message");
使用'IoC',以及...曾爲我的情況,用'城堡Windsor'。有一個'私人靜態LogWriter'和一個接口'LogWriter'屬性解決了它。 – user919426