views:

286

answers:

1

Hi,

so the story go like this:

i'm using the Logging Enterprise Application block that provided by microsoft to log events in our application.

The goal is to enabled event viewer logging at loading and then turn it off.

What i did is to add EventLog TraceListener which write all our logs into the event viewer. What i'm trying to do is to disable it after the application was done loading. The only way that i can thing about is to add a LogEnabled Filter and then turn it off.

However, i don't know how to access this filter at run time and disable log on this listener.

if you have an idea, please share.

Thanks

A: 

Enterprise Library logging offers a bunch of bits to twiddle. So, before getting into changing the configuration maybe some of the out of the box settings can let you do what you want?

It sounds to me like you want your application to log some messages during startup and then not to log any more messages. I'm assuming that you explicitly know when you are done logging startup messages.

If the above is true I think the easiest way would be to use the LogEntry Priority property.

In the configuration file setup a Priority Filter with a minimum priority of 2:

<logFilters>
  <add minimumPriority="2" maximumPriority="2147483647" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.PriorityFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
    name="Priority" />
  <add enabled="true" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null"
    name="LogEnabled Filter" />
</logFilters>

Then during startup use a priority of 2 (or greater) to log your messages. Then when you know you are done set the priority down to 1.

I would encapsulate it better but a code sample would look like:

public class MyApp
{
    public static int LogPriority = 2;
    public static readonly string MyAppCategory = "MyAppCategory";

    static void Main()
    {
        Logger.Write("Loading 1...", MyAppCategory, LogPriority);
        // ...       
        Logger.Write("Loading 2...", MyAppCategory, LogPriority);

        // Done loading so turn off logging
        LogPriority = 1;

        Logger.Write("Message not logged", MyAppCategory, LogPriority);
    }
}


Now if you ever want to turn on logging for the rest of your app, just lower the priority filter from 2 to 1 and everything will get logged.

Tuzo