我在使用 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");