logging block - create a log file as of date

Topics: Building and extending application blocks, Logging Application Block
May 14, 2007 at 2:20 PM
Hello, I am using Enterprise Library Jan 2006 release, I need to know is there any way that I can create my log file base on the date, like if my application executes today, I want to generate the log file with the project name and the date for exam (payroll.14.05.2007.log).

Please please any help in this regard would be help full...and thankx this is such a nice place to discuss and get help about enterprise library....good work guys.....

May 14, 2007 at 5:28 PM
Check out this Rolling Log File for Ent Lib 2.0:

http://bloggingabout.net/blogs/erwyn/articles/rolling_file_trace_listener.aspx

Regards,

Dave

_____________________________

David Hayden
Microsoft MVP C#
May 15, 2007 at 6:51 PM
Hi David,

What can you say re EntLib 3.0? I cound't find a way to set the current log file name with today's timestamp. If you set RollInterval=Day, FileName=trace.log and TimeStampPattern="yyyy-MM-dd" the file name trace.2007-05-15.1.log contains log entries of May 14th, i.e. always yesterday's ones.
May 16, 2007 at 3:41 PM
I believe that will always be the case with daily rolling.

The Rolling Log File TraceListener doesn't name the archive file until it is rolling. It will then name the archive file with the current date, which will always be the next days date.

I think it would make more sense if it had the date of log entries, but I have seen this a lot with other products at client sites such that I am immune to it :)

If it bugs you, it is probably a pretty easy source code change. Here is the code that computes the name of the file that you can find in the RollingLogFileTraceListerner class:

public string ComputeArchiveFileName(string actualFileName, DateTime currentDateTime)
{
    string directoryName = Path.GetDirectoryName(actualFileName);
    string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(actualFileName);
    string extension = Path.GetExtension(actualFileName);
    string fileName = fileNameWithoutExtension + "." + currentDateTime.ToString(this.owner.timeStampPattern, CultureInfo.InvariantCulture);
    if (this.owner.rollFileExistsBehavior == RollFileExistsBehavior.Overwrite)
    {
        return Path.Combine(directoryName, fileName + extension);
    }
    int num = FindMaxSequenceNumber(directoryName, fileName, extension);
    return Path.Combine(directoryName, string.Concat(new object[] { fileName, ".", num + 1, extension }));
}

Regards,

Dave

______________________________

David Hayden
Microsoft MVP C#
May 24, 2007 at 7:23 AM
Edited May 24, 2007 at 7:24 AM
Why not always log to "the correct" filename?

Take for example the rolling on date, create the file directly as 2007-05-24.log or similar. When the next day comes start to log to 2007-05-25.log.

That's how I've always done i it ;)

Regards,
Lani
May 25, 2007 at 4:08 PM

Lani78 wrote:
Why not always log to "the correct" filename?

Take for example the rolling on date, create the file directly as 2007-05-24.log or similar. When the next day comes start to log to 2007-05-25.log.

That's how I've always done i it ;)




Lani, are you sure that the date on the file name and log entries date in the file are the same?
Could you share your settings for the rolling listener?

Thanks.
May 28, 2007 at 8:30 AM
Edited May 28, 2007 at 8:32 AM

miksh wrote:

Lani78 wrote:
Why not always log to "the correct" filename?

Take for example the rolling on date, create the file directly as 2007-05-24.log or similar. When the next day comes start to log to 2007-05-25.log.

That's how I've always done i it ;)




Lani, are you sure that the date on the file name and log entries date in the file are the same?
Could you share your settings for the rolling listener?

Thanks.


Hi miksh,

I'm sorry that my message here might have been placed in the wrong place and that I didn't explain myself clearly.

What I meant was that they should have made this the behaviour of the RollingFlatFileTraceListener, like stated by previous posts in this thread this is not the case.

I just took an example of how I always have done logging to file in my own custom classes, not related to Enterprise Library.

Regards,
Lani