2013-11-15 56 views
3

我在爲Enterprise Library 6.0日誌記錄組件設置記錄器時收到異常。在第一次記錄日誌中,它成功了。但對於連續調用它拋出一個異常:The LogWriter is already set.我已經嘗試檢查Logger.Writer是否不爲空。但是這在第一個例子中失敗了,除了我應該使用Logger.SetLogWriter來設置作者之外。entlib 6記錄器實例化中的錯誤

這裏是我的代碼:

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

使用日誌寫(Microsoft.Practices.EnterpriseLibrary.Logging.dll)在耳lier版本,我們用

var logWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>(); 

EnterpriseLibraryContainer返回相同的實例。

但是在EL 6.0中,LibraryContainer被刪除。然而,記錄器似乎只持有一個作者的一次性初始化。

我在靜態變量的幫助下解決了這個問題,來檢查作者是否被初始化。或者,它可以通過單例或靜態實體完成。

1

的答案是使用靜態變量,以獲得只有一個寫進程的實例

做這樣的:

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); 

    } 

} 
+0

使用'IoC',以及...曾爲我的情況,用'城堡Windsor'。有一個'私人靜態LogWriter'和一個接口'LogWriter'屬性解決了它。 – user919426

4

試試這個:

IConfigurationSource configurationSource = ConfigurationSourceFactory.Create(); 
LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource); 
Logger.SetLogWriter(logWriterFactory.Create(), false); 
+0

不要忘記添加一個對'System.Configuration'的引用到你的項目:)(這是'LogWriterFactory'居住的地方) – async

0
Logger.SetLogWriter(logWriterFactory.Create(), false); 

默認throwIfSet標誌設置爲true,並且如果日誌w riter已經設置。

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");