Writing to a file and a database results in different values for some fields

Topics: Logging Application Block
Feb 13, 2013 at 6:00 PM
I'm using Enterprise Library 5.0 in my class library. I write messages to a database and to a rolling flat file. I create a log entry:
LogEntry logEntry = new LogEntry();
logEntry.AppDomainName = appDomain;
logEntry.Priority = 4;
logEntry.Title = "title";
logEntry.MachineName = "machinename";

I see the value I assigned to AppDomainName or MachineName in the database. But in the file they still appear with the default values. It also ignores the formatter I put in the config file.
Any help will be very appreciated.
Feb 13, 2013 at 10:34 PM
Edited May 1, 2013 at 9:51 PM
You will need to use a Text Formatter to specify your custom AppDomainName and MachineName values. The default template uses the tokens {localMachine} and {localAppDomain} which will read the settings from the runtime environment. However, you can use the tokens {machine} and {appDomain} to use the values specified in the LogEntry.

In configuration this would look something like:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
    </configSections>
    <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
        <listeners>
            <add name="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                formatter="Text Formatter" />
        </listeners>
        <formatters>
            <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                template="Timestamp: {timestamp}&#xA;Message: {message}&#xA;Category: {category}&#xA;Priority: {priority}&#xA;EventId: {eventid}&#xA;Severity: {severity}&#xA;Title:{title}&#xA;Machine: {localMachine}&#xA;App Domain: {localAppDomain}&#xA;ProcessId: {localProcessId}&#xA;Process Name: {localProcessName}&#xA;Thread Name: {threadName}&#xA;Win32 ThreadId:{win32ThreadId}&#xA;Extended Properties: {dictionary({key} - {value})}"
                name="Default Text Formatter" />
            <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.505.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
                template="Timestamp: {timestamp}&#xA;Message: {message}&#xA;Category: {category}&#xA;Priority: {priority}&#xA;EventId: {eventid}&#xA;Severity: {severity}&#xA;Title:{title}&#xA;Machine: {machine}&#xA;App Domain: {appDomain}&#xA;ProcessId: {localProcessId}&#xA;Process Name: {localProcessName}&#xA;Thread Name: {threadName}&#xA;Win32 ThreadId:{win32ThreadId}&#xA;Extended Properties: {dictionary({key} - {value})}"
                name="Text Formatter" />
        </formatters>
        <categorySources>
            <add switchValue="All" name="General">
                <listeners>
                    <add name="Rolling Flat File Trace Listener" />
                </listeners>
            </add>
        </categorySources>
        <specialSources>
            <allEvents switchValue="All" name="All Events" />
            <notProcessed switchValue="All" name="Unprocessed Category" />
            <errors switchValue="All" name="Logging Errors &amp; Warnings">
            </errors>
        </specialSources>
    </loggingConfiguration>
</configuration>
If you are still having issues, then post your configuration so we can pinpoint the issue.

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Feb 13, 2013 at 10:51 PM
Randy, thanks a lot!! This is working now.