Rolling FlatFileListener date issue

Topics: Logging Application Block
Sep 9, 2011 at 10:56 AM

Ok, i am facing this strange issue.....i am using Enterprise Library 5.0....i am trying to use the rolling flat file listener....i have it configured as such.....

<listeners>
      <add name="RFFTL"
           type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
           listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
           fileName="rolling.log"
           rollFileExistsBehavior="Increment"
           rollInterval="Day"
      />
</listeners>

as can be seen that my flat file increments every day but the problem i have with it is that if today is 9th september.....todays file will be named rolling.log and yesterdays file will be renamed as rolling__20110909.txt (meaning yesterday's log file has today's date)....anyway i can get over this problem ??

I don't mind today's file being named as rolling.log but yest or any previous files should be named on the date they were created not the date they were rolled over. Thanks.

Sep 9, 2011 at 1:40 PM
Edited Sep 9, 2011 at 1:41 PM

You can modify this behavior by going in to the source code of Ent Lib 5; the file of interest here is the RollingFlatFileTraceListener.cs file of the Logging Application Block. Take a look at this method of that file and the one I highlighted in bold; I believe it's what you need to edit to get the behavior you want.

void SafeMove(string actualFileName,
                          string archiveFileName,
                          DateTime currentDateTime)
            {
                try
                {
                    if (File.Exists(archiveFileName))
                    {
                        File.Delete(archiveFileName);
                    }
                    // take care of tunneling issues http://support.microsoft.com/kb/172190
                    File.SetCreationTime(actualFileName, currentDateTime);
                    File.Move(actualFileName, archiveFileName);
                }
                catch (IOException)
                {
                    // catch errors and attempt move to a new file with a GUID
                    archiveFileName = archiveFileName + Guid.NewGuid().ToString();

                    try
                    {
                        File.Move(actualFileName, archiveFileName);
                    }
                    catch (IOException) { }
                }
            }
Sep 12, 2011 at 6:10 AM

ok....thanks.....i will try modifying that file and see whether it works or not.....thanks for your help...

Sep 12, 2011 at 6:40 AM

You're welcome. :)

Sep 19, 2011 at 11:21 AM

hi,

i have modified the file and added it to my project by creating a new library but the thing is that it is still giving the original problem....and when i try to debug it gives me an error related to UnityContainer.cs as below :

The debugger could not locate the source file 'e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs'.

So i am basically stuck....any ideas or suggestions.....

Sep 19, 2011 at 1:40 PM

Hi,

If that is the case then perhaps you can create your own custom trace listener. Copy the behavior of the rollling flat file trace listener and just modify it to suit your needs and use that custom trace listener.

My two bits :-)

Sep 19, 2011 at 1:44 PM

Hi,

Thats what i did...i created my own custom using the code from the above location...and added it to my library.....but it does not give the desired output i.e. the dates.....and also gives the unity container issue....

Sep 19, 2011 at 2:26 PM

Hmm...I see. Can you send me your project?

Sep 20, 2011 at 6:02 AM

Thanks for the reply, can you give me your email id or someplace where i can upload it ?

Sep 20, 2011 at 6:06 AM

I PMed you my email address. Send it there

Sep 20, 2011 at 7:55 AM

OK....done.....pls go through it and reply back....thanks for your effort!!! :)

Sep 20, 2011 at 10:30 AM

Hi,

Ok, I've noticed some errors in the way you did your project. I thought that you were making changes to the Ent Lib source code itself, not copying the file to a custom project and then building it. In light of this, let's take it step by step:

First off, download the Ent Lib 5 source code. You can find it here: http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=15104

Once you're done installing it, in your Documents folder, you should see a folder named "EntLib50Src"; that contains the source code of Enterprise Library 5.

Next, open the Blocks folder and locate the sln file for the entire Enterprise Library 5. It is named "EnterpriseLibrary.2010.sln". Double-click it to fire up the master project (The master project has 24 projects in it).

Before I go any further, I would like to point out that you can add your web app project into the Enterprise Library solution so that you have full debugging control. You can do that so you can freely break and step into the Ent Lib code. If you decide to do this, add the necessary references to logging in your web app by choosing it in the Projects tab. Ok, moving on...

Locate the RollingFlatFileTraceListener.cs file. Next, set some breakpoints in the PerformRoll method. I believe that your answer lies within that method. Modify the necessary code.

Once you're done, build the Ent Lib project (without your web app). You may want to use your own snk key though.

Hope this helps! :-)

 

 

 

Sep 20, 2011 at 1:03 PM

Ok....thanks for the suggestions....first of all, tell me what is wrong with the way i have created the project cos i have followed the same method to create a FormattedDatabaseTraceListener and it worked and technically this should work....right ?? just asking...

secondly, i will try the way you have mentioned and see what happens....

Sep 20, 2011 at 2:25 PM

To answer your question, I recommended those steps so that we can get rid of the UnityContainer issues you were experiencing when you were debugging.

Jan 27, 2012 at 5:47 AM
Edited Jan 27, 2012 at 5:48 AM

my flat file increments every day but the problem i have with it is that if today is 9th september.....todays file will be named rolling.log and yesterdays file will be renamed as rolling__20110909.txt (meaning yesterday's log file has today's date)....anyway i can get over this problem ??

I don't mind today's file being named as rolling.log but yest or any previous files should be named on the date they were created not the date they were rolled over. Thanks.

 

 

Please Help me .itz urgent

Jan 29, 2012 at 2:56 AM

I hate to say it but the behavior you are describing is the way it works.  The only way I know of to get the behavior you want with using the date of the file creation would be to write your own trace listener.  You could base it off of the current rolling flat file trace listener code.  If you only need to support rolling on the day then it would be pretty easy to do (just subtract a day from the current date) but the actual rolling flat file trace listener supports many different rolling scenarios so it's not that simple in the general case.

The easiest way might be (in a new trace listener implementation) to create the "active" log file with the "rolled format" initially and when it's time to roll to a new file just close that file and open a new file. 

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

Jan 30, 2012 at 2:58 AM

First of all Thanks for your reply.......

This is my logging configuration in web.config..

Can u please help me to modify this with the way u have described..........

 

  <loggingConfiguration name="" tracingEnabled="true" defaultCategory="General"
    revertImpersonation="false">
    <listeners>
      <add name="Rolling Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        fileName="ErrorLogs5\trace.log" footer="" formatter="Text Formatter"
        header="" rollFileExistsBehavior="Overwrite"   rollInterval="Midnight"
        timeStampPattern="yyyy-MM-dd" maxArchivedFiles="7" traceOutputOptions="None"
        filter="All" />
    
      <add name="Flat File Trace Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
      fileName="ErrorLogs5\trace.log" />
   
    </listeners>
    <formatters>
      <add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        template="{timestamp(local:yyyy-MM-dd HH-mm-ss)}  {message}"
        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">
        <listeners>
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </allEvents>
      <notProcessed switchValue="All" name="Unprocessed Category">
        <listeners>
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </notProcessed>
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Flat File Trace Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>

 

Thanks in advance......

Feb 1, 2012 at 1:13 AM

What I was describing was not a configuration change but a coding change to create a custom trace listener.

The easiest way would probably be to use the RollingFlatFileTraceListener as a starting point to add your functionality.  Or you could take a look at the Enterprise Library Extensibility Hands-on Labs.

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

Feb 3, 2012 at 7:33 AM

 i didnt get wot u have told .....can u help me to change my code..

 

public static void WriteError(string Message)
    {
     

    LogEntry logEntry = new LogEntry();

                logEntry.Message = "Error in: " + System.Web.HttpContext.Current.Request.Url.ToString() +
                ". Error Message:" + Message.ToString();
               
                LogWriter myLogWriter = EnterpriseLibraryContainer.Current.GetInstance<LogWriter>();
                myLogWriter.Write(logEntry);
        
   
            }

Thanks in advance.........

Feb 3, 2012 at 2:14 PM

It's not a matter of changing the code you posted.  That code is just how data is written not how the internal Trace Listener logic is performed.

The RollingFlatFileTraceListener is a TraceListener provided by Enterprise Library that logs to a file and performs rolling action based on a number of criteria.  Based on your previous post you would like to change the way files are named when they are rolled to a different convention than the one used by the RollingFlatFileTraceListener.

The only way to do this (that I can think of) is  to write your own version of a RollingFlatFileTraceListener.  The best way would be to download the source code for Enterprise Library 5 (http://www.microsoft.com/download/en/details.aspx?id=15104 ) and copy and modify to create your own Trace Listener based on the RollingFlatFileTraceListener.  The Enterprise Library Extensibility Hands-on Labs should provide guidance as well.

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

Feb 4, 2012 at 5:55 AM

K...Thanks for your reply.......I wil try with that..