Custom Trace listener

Topics: Building and extending application blocks, Logging Application Block
Jan 7, 2009 at 2:32 PM
Hi
I am trying to create a custom trace listener that will write log entry . I made the dll and added to the application. But it wouldnt write anything to database or eventlog.I am not seeing any error
        public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
        {AuditLogEntry ale = data as AuditLogEntry;
if (ale == null)
            {

                //Not an audit log entry

                return;

            }

            IAuditLoggerExpressionGroup logGroup;

            if (!AuditLoggerExpressionCache.Instance.TryGetValue(ale.AuditCategory, out logGroup))
            {

                //Category not registered

                return;

            }
 using (StoredProcedure sp = new StoredProcedure(logGroup.Procedure))
            {

                foreach (string key in logGroup.Keys)
                {

                    sp.AddParameter(key, logGroup.GetValue(key, ale));

                }
                sp.Execute();

            }

       
            }

configuration
  <loggingConfiguration name="Logging Application Block" tracingEnabled="true"
    defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add fileName="CustomLog{Date}.txt" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.CustomTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        traceOutputOptions="None" filter="All" type="LoggingToDB.DBAuditLoggerTraceListener, AuditingTraceListener, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d1cf042f53470a8f"
        name="Custom Trace Listener" initializeData="" formatter="Text Formatter" />
    </listeners>
    <formatters>
      <add template="Timestamp: {timestamp}&#xD;&#xA;Message: {message}&#xD;&#xA;Category: {category}&#xD;&#xA;Priority: {priority}&#xD;&#xA;EventId: {eventid}&#xD;&#xA;Severity: {severity}&#xD;&#xA;Title:{title}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Application Domain: {appDomain}&#xD;&#xA;Process Id: {processId}&#xD;&#xA;Process Name: {processName}&#xD;&#xA;Win32 Thread Id: {win32ThreadId}&#xD;&#xA;Thread Name: {threadName}&#xD;&#xA;Extended Properties: {dictionary({key} - {value}&#xD;&#xA;)}"
        type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        name="Text Formatter" />
    </formatters>
    <categorySources>
      <add switchValue="All" name="General" />
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events">
        <listeners>
          <add name="Custom Trace Listener" />
        </listeners>
      </allEvents>
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Custom Trace Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>

PageLoad
  AuditLogEntry entry = new AuditLogEntry("custom message","custom category");
            entry.Message = "Sample Log Message";
            entry.Categories.Add("Trace");
                       Logger.Write(entry);


Anyone please help me out. Thanks
Jan 8, 2009 at 1:25 AM
You don't have a "Trace" category.  You should add the "General" category instead of "Trace"

Sarah Urmeneta
Global Technology & Solutions
Avanade, Inc.
entlib.support@avanade.com