我在为企业库 6.0 日志记录组件设置日志编写器时遇到异常。第一次出现日志记录时,它就会成功。但是对于连续调用,它会抛出异常:The LogWriter is already set.
我尝试检查 Logger.Writer 是否不为空。但这首先失败了,除了我应该使用 Logger.SetLogWriter 设置编写器。
这是我的代码:
if (Logger.Writer == null) // fails on first call
{
IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
Logger.SetLogWriter(logWriterFactory.Create()); // fails on subsequent calls.
}
试试这个:
IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
Logger.SetLogWriter(logWriterFactory.Create(), false);
我在使用 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");
答案是使用静态变量,以便只获取一个日志编写器实例
这样做:
public class BaseController : Controller
{
private static ExceptionManager _exceptionManager;
private static LogWriter _logWriter;
public ExceptionManager ExceptionManager
{
get
{
IUnityContainer container = new UnityContainer();
try
{
if (_exceptionManager == null)
{
IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
ExceptionPolicyFactory exceptionFactory = new ExceptionPolicyFactory(configurationSource);
if (configurationSource.GetSection(LoggingSettings.SectionName) != null)
{
LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
Logger.SetLogWriter(logWriterFactory.Create());
}
container.RegisterInstance<ExceptionManager>(exceptionFactory.CreateManager());
_exceptionManager = container.Resolve<ExceptionManager>();
return _exceptionManager;
}
else
{
return _exceptionManager;
}
}
finally
{
((IDisposable)container).Dispose();
}
}
set
{
_exceptionManager = value;
}
}
/// <summary>
/// Used to log entries in the log file
/// </summary>
public LogWriter LogWriter
{
get
{
IUnityContainer container = new UnityContainer();
try
{
if (_logWriter == null)
{
IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
Logger.SetLogWriter(logWriterFactory.Create());
// Singleton
container.RegisterInstance<LogWriter>(logWriterFactory.Create());
_logWriter = container.Resolve<LogWriter>();
return _logWriter;
}
else
return _logWriter;
}
finally
{
((IDisposable)container).Dispose();
}
}
set
{
_logWriter = value;
}
}
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("es");
Thread.CurrentThread.CurrentCulture = new CultureInfo("es");
if (Session.Count == 0)
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary { { "controller", "Account" }, { "action", "Login" } });
}
}
protected override void OnException(ExceptionContext filterContext)
{
this.ExceptionManager.HandleException(filterContext.Exception, "AllExceptionsPolicy");
//Show basic Error view
filterContext.ExceptionHandled = true;
//Clear any data in the model as it wont be needed
ViewData.Model = null;
//Show basic Error view
View("Error").ExecuteResult(this.ControllerContext);
}
}
您是否检查过您的web.config(如果是 asp.net)或app.config(如果是控制台或Windows应用程序)中没有任何LogWriter设置。
另请参阅如何在此线程中使用 LogWriter (Microsoft.Practices.EnterpriseLibrary.Logging.dll)
如何在 EntLib 日志应用块中为跟踪侦听器设置和使用多个格式化程序
在早期版本中,我们使用
var logWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
EnterpriseLibraryContainer 返回相同的实例。
但是在EL 6.0中,LibraryContainer被丢弃了。但是,记录器似乎只包含一个具有一次性初始化的编写器。
我在静态变量的帮助下解决了这个问题,以检查编写器是否已初始化。 或者,它可以通过单例或静态实体来完成。
Logger.SetLogWriter(logWriterFactory.Create(), false);
默认情况下,throwIfSet
标志设置为 true
,如果已设置日志编写器,则会引发异常。