none
RollOver logic for logs with FileLogTraceListener RRS feed

  • Question

  • I am trying the following logging behavior:

    •Create a new file daily

    •When log file exceeds MaxFileSize, create a new file for that day with a new version number.

    The first part works fine by setting LogFileCreationSchedule to LogFileCreationScheduleOption.Daily. My code looks something like this.

    public class MyTraceListener : FileLogTraceListener
     {
        public MyTraceListener (): base()
           {
            this.LogFileCreationSchedule = LogFileCreationScheduleOption.Daily;
           }
     }

    For the second part i was trying to write my own code to add the counter . But when i was going through the below link in msdn , it is mentioned that it would add an integer to the file name but which doesnt  happening.

    https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.logging.filelogtracelistener(v=vs.110).aspx

    Few lines from the above link::

    *Archival functionality. The log files generated by this class are named according to the base name and the date, along with a number to distinguish the log file from successive versions of the log. New log files are created on an as-needed basis.The explicit form of the file name is baseName[-dateStamp][-version].log, where:

    ◦The baseName part is the fundamental log name, specified by the BaseFileName property.

    ◦The dateStamp part has the format "YYYY-MM-DD", and it is shown when LogFileCreationSchedule is Daily or Weekly.

    ◦If more than one log file is needed with the same baseName and dateStamp, the version part, a positive Integer, is added to the file name.

    Anyone tried using the Daily option and it created the file with number appended to the logfilename (DTM-2015-06-24_1) when it exceeded max size of 5MB which is default value.*


    Krrishna

    Wednesday, June 24, 2015 4:17 PM

Answers

  • Hello Krrrishna,

    >>Anyone tried using the Daily option and it created the file with number appended to the logfilename (DTM-2015-06-24_1) when it exceeded max size of 5MB which is default value.*

    For the FileLogTraceListener class, if the log file size is greater than what the MaxFileSize property allows, it would just throw an exception or silently discard these new added messages according to the FileLogTraceListener.DiskSpaceExhaustedBehavior Property:

    https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.logging.filelogtracelistener.diskspaceexhaustedbehavior(v=vs.110).aspx

    >>◦If more than one log file is needed with the same baseName and dateStamp, the version part, a positive Integer, is added to the file name.

    This behavior would happen when the below scenario occurs:

    The class creates a new log file using the next available name if the file is being used by another process

    For testing it, you could use the FileInfo class to open an stream and then using the FileLogTraceListener or TraceSource to write some message to the log file as below:

    FileInfo file = new FileInfo("Test-2015-06-25.log");
    
    
                    FileStream stream = file.OpenRead();
    
    
                    TraceSource TraceSourceInAppConfig = new TraceSource("DefaultSource");
    
    
                    TraceSourceInAppConfig.TraceInformation("Info message");
    
    
                    TraceSourceInAppConfig.TraceEvent(TraceEventType.Error, 3, "Error message");
    
    
                    TraceSourceInAppConfig.TraceData(TraceEventType.Information, 4, new int[] { 1, 2, 3 });
    
    
                    TraceSourceInAppConfig.TraceEvent(TraceEventType.Warning, 2, "Warning message is …");
    
    
                    TraceSourceInAppConfig.Flush();
    
    
                    TraceSourceInAppConfig.Close();
    

    It then would create a file as:

    Test-2015-06-25-1.log

    >> When log file exceeds MaxFileSize, create a new file for that day with a new version number.

    So I am afraid the FileLogTraceListener may not be able to handle this scenario automatically. You could set the DiskSpaceExhaustedBehavior to throw an exception, catch it and initialize an new FileLogTraceListener with a proper name.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, June 25, 2015 5:03 AM
    Moderator