我在为Enterprise Library 6.0日志记录组件设置logwriter时遇到异常。在第一次发生日志记录时,它会成功。但对于连续调用,它会引发异常: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.
}
答案 0 :(得分:4)
试试这个:
IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
Logger.SetLogWriter(logWriterFactory.Create(), false);
答案 1 :(得分:2)
使用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");
答案 2 :(得分:1)
答案是使用静态变量以便只获取一个logwriter实例
这样做:
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);
}
}
答案 3 :(得分:0)
您是否检查过web.config(如果是asp.net)或app.config(如果是控制台或Windows应用程序)中没有任何LogWriter设置。
另请参阅此线程中如何使用LogWriter(Microsoft.Practices.EnterpriseLibrary.Logging.dll)
How to set and use more than one formatter for a trace listener in EntLib log app block
答案 4 :(得分:0)
在早期版本中,我们使用了
var logWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
EnterpriseLibraryContainer返回相同的实例。
但是使用EL 6.0,LibraryContainer将被删除。但是,Logger似乎只能保留一个一次性初始化的编写器。
我在静态变量的帮助下解决了这个问题,以检查编写器是否已初始化。或者,它可以通过单例或静态实体来完成。
答案 5 :(得分:0)
Logger.SetLogWriter(logWriterFactory.Create(), false);
默认情况下throwIfSet
标志设置为true
,如果已经设置了日志编写器,则会抛出异常。