Gerald Hughes
Gerald Hughes

Reputation: 6157

The Enterprise Library Logging Application Block - System.InvalidOperationException: The LogWriter is already set

I'm trying to use the the Logging Exception Handler from The Enterprise Library, but once in a while when I refresh the page (obviously), I get this error:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
Exception Details: System.InvalidOperationException: The LogWriter is already set.

Code

protected void Page_Load(object sender, EventArgs e)
        {
            Logger.SetLogWriter(new LogWriterFactory().Create());
            LogEntry entry = new LogEntry();
            entry.Message = "I am logging";
            Logger.Write(entry);

            if (!Page.IsPostBack)
            {
            }
        }           

Stack trace

[InvalidOperationException: The LogWriter is already set.]
Microsoft.Practices.EnterpriseLibrary.Logging.Logger.SetLogWriter(LogWriter logWriter, Boolean throwIfSet) +165
ASPUserManager.Presentation.AspNetUI.GridView.Page_Load(Object sender, EventArgs e) in \AspNetUI\GridView.aspx.cs:20
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
System.Web.UI.Control.OnLoad(EventArgs e) +92
System.Web.UI.Control.LoadRecursive() +54
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

I'm thinking maybe I should check if LogWriter is already set?

I'm new to The Enterprise Library Logging Application Block and I'm not sure how to fix this, does anyone have any idea?

Upvotes: 1

Views: 6430

Answers (2)

Darshana
Darshana

Reputation: 31

You need to set logwritter false Logger.SetLogWriter(logWriterFactory.Create(),false);

By default throwIfSet is set to true and throws error if logwriter is already set.

Upvotes: 3

Gerald Hughes
Gerald Hughes

Reputation: 6157

You only need to bootstrap the block once -- once the LogWriter is set it is stored internally and used for subsequent Write() calls. Usually you would do this at application startup (e.g. in global.asax Application_Start).

protected void Application_Start()
{
    Logger.SetLogWriter(new LogWriterFactory().Create());
}

The above code loads the configuration and sets the LogWriter. Now in your page you can call Logger.Write().

Incidently SetLogWriter does accept a boolean indicating whether to throw if the LogWriter was already set so you can override this throw behavior.

However, in your case you don't want to do that in Page_Load. It might avoid the error you are seeing but in a multi-threaded environment (like a a web app) you will (probably) see other strange behavior because the SetLogWriter resets the internal configuration and you could have issues with file locking or other issues due to a disposed LogWriter.

Source: entlib forums

Upvotes: 4

Related Questions