AppDomainName customization with Enterprise Library on an ASP.Net MVC App

Topics: Logging Application Block
Sep 11, 2013 at 4:18 AM
I been using Microsoft’s Enterprise Library for Logging. I want to be able to query the Log table based on the AppDomainName, which is not a problem on executables as they log the AssemblyName as the AppDomainName. However, when it comes to an MVC application, I get an arbitrary name, session id I think in the form such as /LM/W3SVC/2/ROOT/AppNameHere-1-130227215731585214

Does anyone have any direction how I can configure this to be a constant name regardless of the session, ie. “AppNameHere”

Thank you
Editor
Sep 11, 2013 at 4:47 AM
If you create and use a LogEntry and use the LogWriter.Write(LogEntry) overload (instead of the other LogWriter.Write overloads) you can set the AppDomainName (overriding the default AppDomainName). For example:
    LogEntry logEntry = new LogEntry 
    { 
        Message = "Error Message", 
        Severity = TraceEventType.Error,
        AppDomainName = "MyAppDomain"
    };
            
    Logger.Write(logEntry);
  
~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Sep 11, 2013 at 12:51 PM
Is there any way to do this with the config xml?
Editor
Sep 11, 2013 at 2:20 PM
There is nothing available through Enterprise Library to automatically set the AppDomainName property. You can only hard code the value in the Formatter but that will appear in the FormattedMessage column and not the AppDomainName column. You could read the value from the app/web.config and then set it in the LogEntry:
  <appSettings>
    <add key="AppDomainName" value="MyOtherAppDomain"/>
  </appSettings>

    LogEntry logEntry = new LogEntry 
    { 
        Message = "Error Message", 
        Severity = TraceEventType.Error,
        AppDomainName = ConfigurationManager.AppSettings["AppDomainName"]
    };

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Sep 11, 2013 at 10:36 PM
Edited Sep 11, 2013 at 10:36 PM
Much appreciated, here is what I came up with and I have a follow-up

I currently use this method
    Logger.Write(message, 
                "Billing Status",
                priority, 
                eID,
                eventType, 
                message);

        }
By overloading and setting my AppDomainName , I don't have a property for my category "Billing Status"
LogEntry logEntry = new LogEntry
            {
                Message = message,
                Priority = priority,
                EventId = eID,
                Severity = TraceEventType.Error,
                AppDomainName = ConfigurationManager.AppSettings["AppDomainName"]
                };
How can I overload the Category property so I can log my "Billing Status"?

Thanks... and cheers!
Editor
Sep 12, 2013 at 5:47 AM
How can I overload the Category property so I can log my "Billing Status"?

There are a few ways to do it:

Set categories using Object Initializer:
    LogEntry logEntry = new LogEntry
    {
        Message = "Error Message",
        Severity = TraceEventType.Error,
        AppDomainName = ConfigurationManager.AppSettings["AppDomainName"],
        Categories = new string[] { "Billing Status" }
    };

or add category to the created LogEntry:
    LogEntry logEntry = new LogEntry
    {
        Message = "Error Message",
        Severity = TraceEventType.Error,
        AppDomainName = ConfigurationManager.AppSettings["AppDomainName"]
    };

    logEntry.Categories.Add("Billing Status");

or use a constructor along with an Object Initializer (or set the property after creating the LogEntry):
    LogEntry logEntry = new LogEntry("Error Message", "Billing Status", priority, eID, eventType, title, null)
    {
        AppDomainName = ConfigurationManager.AppSettings["AppDomainName"]
    };

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to