How to add dynamic Date in Logging Path ?

Topics: Logging Application Block
Apr 10, 2012 at 9:26 AM
Edited Apr 10, 2012 at 9:57 AM

I'm try to add dynamic date in Logging Path but it's not working

This is .config

 

      <add name="EnterpriseLibraryRollingFlatFileDestination" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging"
        fileName="C://temp//{Date}//McEnterpriseLibError.log" formatter="Raw Message Text Formatter"
        rollFileExistsBehavior="Increment" rollInterval="Midnight" rollSizeKB="1000"
        timeStampPattern="yyyy-MM-dd" traceOutputOptions="None" filter="All" />

 

 

and Result of this config is

Image and video hosting by TinyPic

 

expect result is

 

C:\temp\2012-04-08\McEnterpriseLibError.log
C:\temp\2012-04-09\McEnterpriseLibError.log
C:\temp\2012-04-10\McEnterpriseLibError.log

 

 

use entlib5

Thanks 

Apr 10, 2012 at 3:20 PM

Enterprise Library does support the expansion of environment variables in file names.  Note that this expansion only occurs when the Trace Listener is created (on first use).  Date is also not an environment variable that is available (Enterprise Library uses the Environment.ExpandEnvironmentVaraibles method).  So even if date was available it would not be dynamic since it is initialized only once.  Also, the format for environment variables is %variableName%.

The options I can think of are:

  1. Create your own environment variable and restart the application at midnight every day
  2. Create a separate process to archive files to subfolders
  3. Use programmatic logging where you can control the exact file name via code
  4. Create a custom Trace Listener that implements the behavior you desire 

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

Apr 10, 2012 at 3:32 PM

Sorry, have any ways to do not restart application at midnight because my application would be run 24 hour.

Thanks a lot.

Apr 10, 2012 at 5:17 PM

Options 2-4 are still available.  Also, what you really need is not to restart the application but to reset the EnterpriseLibraryContainer; the easiest way to do that is via an application restart.  However, you can manually reset the container but you will need to know when to execute that logic (either a timer, or a date check on every log or some external action that triggers something in your app).  The logic would roughly look like this:

            Environment.SetEnvironmentVariable("MyDate", DateTime.Now.ToString("yyyy-MM-dd"));

            var logger = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
            logger.Write("1", "General");

            // next day so this code is triggered
            Environment.SetEnvironmentVariable("MyDate", DateTime.Now.ToString("yyyy-MM-dd"));

            IUnityContainer container = new UnityContainer().AddExtension(new EnterpriseLibraryCoreExtension());
            EnterpriseLibraryContainer.Current = new UnityServiceLocator(container);

            logger = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
            logger.Write("2", "General");

And in the config you would have the fileName set to be: fileName="C:\temp\%MyDate%\McEnterpriseLibError.log"

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

Apr 10, 2012 at 5:24 PM

i'll try to do this

thanks again.