locked
Current date in the name of a log file RRS feed

  • Question

  • Hi,

    Is it possible to add current date to the name of the file where SharedListener store logs? At present my application.exe.config file looks like following:

    <system.diagnostics>
         
          <sources>
            ...
          </sources>
         
          <sharedListeners>
            <add name="txtListener"
                 initializeData="ClientLog.txt"
                 traceOutputOptions="DateTime, Timestamp"
                 type="System.Diagnostics.TextWriterTraceListener" />
          </sharedListeners>
          ....

    The name of file is static, all logs are written into ClientLog.txt file. SharedListener allways write to the same file so after some short time this file will be very big and I'd like to avoid this. I'd like to create files like

    ClientLog_08-10-2009.txt
    ClientLog_09-10-2009.txt
    ClientLog_10-10-2009.txt
    ...
    etc.

    but the name of the file should change automatically when date change.
    This would also help me to find the logs, beacuse usually I know the date when some problems appear. Is it possible to add such functionality? It would be also good if some old log files would be deleted automatically. For example onlu logs from last two weeks are intreresting form me, so older log file clould be deleted when new file is created.

    Thanks in advance
    Mariusz

    Mariusz
    Thursday, October 8, 2009 9:52 AM

Answers

  • Thank you for your answer,

    I've also find another way:
    In my project I've added reference to Microsoft.VisualBasic. Then in my app.config file I can use following configuration:

     <sharedListeners>
    <add name="txtRolingListener"
               type="Microsoft.VisualBasic.Logging.FileLogTraceListener,
                     Microsoft.VisualBasic,
                     Version=8.0.0.0,
                     Culture=neutral,
                     PublicKeyToken=b03f5f7f11d50a3a,
                     processorArchitecture=MSIL"
               initializeData="FileLogWriter"
               location="Custom"
               customLocation ="Logs"
               logFileCreationSchedule="Daily"
               traceOutputOptions="DateTime,Timestamp" />
        </sharedListeners>

    Ofcourse some source have to be configured to send data to this listener.

    Regards
    Mariusz
    • Marked as answer by wolbiend Monday, October 19, 2009 1:52 PM
    Monday, October 19, 2009 1:50 PM
  • Hi,
    To generate a collection of log files with proper file names, we need to implement new types which derived from TextWriterTraceListener or FileStream.
    Alothough following link shows how to customize FileStream to generate fix-sized log files, it may give some ideas to you:
    http://www.codeproject.com/KB/dotnet/customnettracelisteners.aspx

    Thanks,
    Eric
    Please remember to mark helpful replies as answers and unmark them if they provide no help.
    • Marked as answer by eryang Thursday, October 15, 2009 3:17 AM
    Monday, October 12, 2009 8:44 AM

All replies

  • In place on DateTime use DateTime.Now.Tostring("dd-MM-yyyy"); This will give you the log files of different days. If you want to create a new Log file after each hour then you can use DateTime.Now.Tostring("dd-MM-yyyy,H");
    Thanks
    Varun Kumar
    • Proposed as answer by varun007 Thursday, October 8, 2009 11:11 AM
    Thursday, October 8, 2009 11:11 AM
  • Hi Varun,

    Thank you for your answer.
    Do you have any example? I cannot imagine how to add DateTime.Now.Tostring("dd-MM-yyyy"); to the config file? If I add like you suggest then this will look following:

    traceOutputOptions="DateTime.Now.Tostring("dd-MM-yyyy"); , Timestamp"

    Then please look at the quotation marks. I think this is not correct format of config file.

    Regards
    Mariusz


    Mariusz
    Thursday, October 8, 2009 12:47 PM
  • Sorry. I thought you are talking about the class file in c#.
    ok if you want to give the datetime format like that in cnfig file for that here is the best example. You can test this.

    <appender name="rollingFile" type="log4net.Appender.RollingFileAppender,log4net">
     
    <param name="File" value="c:\\ProjectX\\Log\\log.txt"/>
     
    <param name="AppendToFile" value="true"/>
     
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
     
    <param name="RollingStyle" value="Date"/>
     
    <param name="DatePattern" value="yyyy.MM.dd,H"/>
     
    <param name="StaticLogFileName" value="true"/>
     
    <layout type="log4net.Layout.PatternLayout,log4net">
       
    <param name="ConversionPattern" value="%date [%thread] %-5level %logger - %message%newline"/>
     
    </layout>
    </appender>
    Thanks

    Varun Kumar
    • Proposed as answer by varun007 Thursday, October 8, 2009 1:01 PM
    Thursday, October 8, 2009 1:01 PM
  • I don't use log4net . Your descripion is about configuration for log4net but I use <system.diagnostics>.
    This is something comletely different.
    Mariusz
    Friday, October 9, 2009 9:43 AM
  • Hi,
    To generate a collection of log files with proper file names, we need to implement new types which derived from TextWriterTraceListener or FileStream.
    Alothough following link shows how to customize FileStream to generate fix-sized log files, it may give some ideas to you:
    http://www.codeproject.com/KB/dotnet/customnettracelisteners.aspx

    Thanks,
    Eric
    Please remember to mark helpful replies as answers and unmark them if they provide no help.
    • Marked as answer by eryang Thursday, October 15, 2009 3:17 AM
    Monday, October 12, 2009 8:44 AM
  • Thank you for your answer,

    I've also find another way:
    In my project I've added reference to Microsoft.VisualBasic. Then in my app.config file I can use following configuration:

     <sharedListeners>
    <add name="txtRolingListener"
               type="Microsoft.VisualBasic.Logging.FileLogTraceListener,
                     Microsoft.VisualBasic,
                     Version=8.0.0.0,
                     Culture=neutral,
                     PublicKeyToken=b03f5f7f11d50a3a,
                     processorArchitecture=MSIL"
               initializeData="FileLogWriter"
               location="Custom"
               customLocation ="Logs"
               logFileCreationSchedule="Daily"
               traceOutputOptions="DateTime,Timestamp" />
        </sharedListeners>

    Ofcourse some source have to be configured to send data to this listener.

    Regards
    Mariusz
    • Marked as answer by wolbiend Monday, October 19, 2009 1:52 PM
    Monday, October 19, 2009 1:50 PM