3

在为 Enterprise Library 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.
}
4

6 回答 6

4

尝试这个:

IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
Logger.SetLogWriter(logWriterFactory.Create(), false);
于 2015-02-03T16:35:06.500 回答
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");
于 2016-02-05T04:36:02.077 回答
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);

    }

}
于 2014-02-04T13:57:38.313 回答
0

您是否检查过您的 web.config(如果是 asp.net)或 app.config(如果是控制台或 Windows 应用程序)中没有任何 LogWriter 设置。

另请参阅如何在此线程中使用 LogWriter (Microsoft.Practices.EnterpriseLibrary.Logging.dll)

如何在 EntLib 日志应用程序块中为跟踪侦听器设置和使用多个格式化程序

于 2013-11-15T11:26:53.073 回答
0

在早期版本中,我们使用

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

EnterpriseLibraryContainer 返回相同的实例。

但是在 EL 6.0 中,LibraryContainer 被删除了。然而,Logger 似乎只拥有一个一次性初始化的写入器。

我在静态变量的帮助下解决了这个问题,以检查编写器是否已初始化。或者,它可以通过单例或静态实体来完成。

于 2013-11-15T13:30:30.307 回答
0
Logger.SetLogWriter(logWriterFactory.Create(), false);

默认情况下,如果已经设置了日志写入器,则throwIfSet标志设置为并且将引发异常。true

于 2015-08-11T16:46:37.117 回答