Reputation: 1860
I am getting an exception while setting the logwriter for Enterprise Library 6.0 logging component. On the first occurrence of logging, it succeeds. But for the consecutive calls it throws an exception: The LogWriter is already set.
I have tried with checking if the Logger.Writer is not null. But this fails in the first instance, with an exception that I should set the writer using Logger.SetLogWriter.
Here is my code:
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.
}
Upvotes: 3
Views: 9994
Reputation: 197
I also faced the same problem when used 6.0 version. I solved this by creating my own static logger class and initializing the SetLogWriter() in the constructor. Below is the code:
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);
}
}
}
And simply used like
Logger.Log(ex);
//or
Logger.Log("Your message");
Upvotes: 2
Reputation: 21
Logger.SetLogWriter(logWriterFactory.Create(), false);
by default throwIfSet
flag set to true
and will throw exception if the log writer is already set.
Upvotes: 0
Reputation: 53
Try this:
IConfigurationSource configurationSource = ConfigurationSourceFactory.Create();
LogWriterFactory logWriterFactory = new LogWriterFactory(configurationSource);
Logger.SetLogWriter(logWriterFactory.Create(), false);
Upvotes: 4
Reputation: 11
the answer is to use a static variable in order to get only one instance of logwriter
do it like this:
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);
}
}
Upvotes: 1
Reputation: 1860
In the earlier versions, we used
var logWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
EnterpriseLibraryContainer returns the same instance.
But with EL 6.0, LibraryContainer is dropped. However the Logger is seems to hold only one writer with one-time initialization.
I solved this with the the help of static variable, to check if the writer is initialized or not. Alternatively, it can be done through singleton or static entity.
Upvotes: 0
Reputation: 1265
Have you checked that there is not any setting of LogWriter in your web.config(if it's asp.net) or app.config(if it's console or windows app.)
Please also see how to use LogWriter (Microsoft.Practices.EnterpriseLibrary.Logging.dll) in this thread
How to set and use more than one formatter for a trace listener in EntLib log app block
Upvotes: 0