Logging with Enterprise Library 3.1

Topics: Logging Application Block
Aug 3, 2012 at 9:49 AM

Hello,

I use the Enterprise Library 3.1 to add log in my application using handler : [Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.LogCallHandler(LogBeforeCall = false)]

That works great !

But I have a question : is it possible to manually add log to the same file ? Some times I would like to add trace in this file generated manually. I try to write directly in the file but it's use "by an other process". Is there a solution using the entreprise library ?

thank you for your help !

Aug 3, 2012 at 1:45 PM

You cannot have two trace listeners writing to the same file.  Also, you will not be able to manually write to the file.

In order to write to the log file that the call handler is writing to use Enterprise Library Logging Application Block to write to the same category as is configured for your Logging Call Handler.

So for example if the configuration looks like:

      <add name="Authorize and Audit">
        <matchingRules>
          <add type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.MatchingRules.NamespaceMatchingRule, Microsoft.Practices.EnterpriseLibrary.PolicyInjection, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null" name="Namespace Matching Rule">
            <matches>
              <add match="PolicyInjectionQuickStart.BusinessLogic" ignoreCase="false" />
            </matches>
          </add>
        </matchingRules>
        <handlers>
          <add name="Logging Handler" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.LogCallHandler, Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null" logBehavior="BeforeAndAfter" beforeMessage="Before" afterMessage="After" includeParameterValues="true" includeCallTime="true" includeCallStack="false" severity="Information">
            <categories>
              <add name="Audit" />
            </categories>
          </add>
          <add name="AuthZ Handler" type="Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers.AuthorizationCallHandler, Microsoft.Practices.EnterpriseLibrary.PolicyInjection.CallHandlers, Version=3.1.0.0, Culture=neutral, PublicKeyToken=null" operationName="{method}" authorizationProvider="RuleProvider" />
        </handlers>
      </add>

Then if you also wanted to write to the same file then use Enterprise Library to write to the "Audit" category.

            Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write("Log some extra information", 
                "Audit", int.MaxValue, 0, TraceEventType.Information, "Additional Logging not a call handler");

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

Aug 3, 2012 at 4:04 PM

thank you very much !

I don't understand where I need to add the configuration. Actually my config is :

 

  <loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add fileName="c:\Log.log" rollSizeKB="128" timeStampPattern="yyyy-MM-dd" rollFileExistsBehavior="Overwrite" rollInterval="None" formatter="Text Formatter" header="----------------------------------------" footer="----------------------------------------" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" name="Rolling Flat File Trace Listener" />
    </listeners>
    <formatters>
      <add template="Date d'execution: {timestamp}&#xD;&#xA;Machine: {machine}&#xD;&#xA;Methode : {property(TypeName)}.{property(MethodName)}&#xD;&#xA;Parametres : {dictionary({key} = {value} , )}&#xD;&#xA;Duree execution : {property(CallTime)}&#xD;&#xA;Valeur retour : {property(ReturnValue)}&#xD;&#xA;Erreur : {property(Exception)}&#xD;&#xA;&#xD;&#xA;" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" name="Text Formatter" />
    </formatters>
    <logFilters>
      <add enabled="true" type="Microsoft.Practices.EnterpriseLibrary.Logging.Filters.LogEnabledFilter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" name="LogEnabled Filter" />
    </logFilters>
    <specialSources>
      <allEvents switchValue="All" name="All Events">
        <listeners>
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </allEvents>
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings" />
    </specialSources>



  </loggingConfiguration>

 Thank you for your help !

Aug 3, 2012 at 5:46 PM

Since you have the allEvents specialSource configured you don't have to do any additional configuration.  Just write a LogEntry/message and it should be directed to the allEvents specialSource (in your case Rolling Flat File Trace Listener).

So you should be able to log anything and have it be directed to the rolling flat file: 

 

Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write("Log some extra information");

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

 

Aug 20, 2012 at 7:09 AM

Hello ! Sorry for the delay, I was on vacation :-)

If I try to add log, I have this value in the log :

Date d'execution: 03/08/2012 15:57:28
Machine: VEPO120
Methode : <Error: property TypeName not found>.<Error: property MethodName not found>
Parametres :
Duree execution : <Error: property CallTime not found>
Valeur retour : <Error: property ReturnValue not found>
Erreur : <Error: property Exception not found>

I think it is normal because there is no method. But how can I see the text I add ? I think I have to change the config to add some value in the template ?

Thank you

Aug 21, 2012 at 1:24 AM

Usually the information you are logging will appear in the Message so you will need to add the message token to the template (e.g. ";Message: {message}").  

I'm just curious if both the LoggingCallHandler information and the additional information is writing to the same file?  

--
Randy Levy
Enterprise Library support engineer
entlib.support@live.com 

 

 

Aug 27, 2012 at 7:12 AM

Thank you very much for your help !

Yes both are written in the same file.

Seb.