Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Best way to schedule rotation of logfile for c# service

Options
  • 01-08-2012 5:28pm
    #1
    Registered Users Posts: 7,501 ✭✭✭


    I have a windows service which logs tasks performed throughout the day to a text file.

    The file is date stamped and i delete the 7th oldest file every day.

    Im currently doing this by checking the oldest file every time i write to the log file and if there is one thats older than 7 days i delete it.

    I think doing this check every time i add a log entry seems like a waste as its only an action that needs to be performed once a day yet i make 1000's of writes to the log file every day.

    Any suggestions on the best way to trigger the rotation? Is there a way to trigger the rotation when the windows system passes midnight?


Comments

  • Registered Users Posts: 1,263 ✭✭✭00sully


    I have a windows service which logs tasks performed throughout the day to a text file.

    The file is date stamped and i delete the 7th oldest file every day.

    Im currently doing this by checking the oldest file every time i write to the log file and if there is one thats older than 7 days i delete it.

    I think doing this check every time i add a log entry seems like a waste as its only an action that needs to be performed once a day yet i make 1000's of writes to the log file every day.

    Any suggestions on the best way to trigger the rotation? Is there a way to trigger the rotation when the windows system passes midnight?

    Log4net or nlog will handle all this headache free and are a known quantity


  • Registered Users Posts: 7,501 ✭✭✭BrokenArrows


    Is rather do this without any 3rd party software.


  • Registered Users Posts: 203 ✭✭Sherfin


    Scheduled task maybe ?

    Or if it needs to be in the service, keep a last run date.
    Whenever getDate() returns higher then you need to run your delete old file method and reset your last run date.

    Hope I read you question right


  • Registered Users Posts: 7,501 ✭✭✭BrokenArrows


    Sherfin wrote: »
    Scheduled task maybe ?

    Or if it needs to be in the service, keep a last run date.
    Whenever getDate() returns higher then you need to run your delete old file method and reset your last run date.

    Hope I read you question right

    Ya i suppose that is a better option. I dont have the file i/o overhead every time.


  • Registered Users Posts: 385 ✭✭nicol


    I have a windows service which logs tasks performed throughout the day to a text file.

    The file is date stamped and i delete the 7th oldest file every day.

    Im currently doing this by checking the oldest file every time i write to the log file and if there is one thats older than 7 days i delete it.

    I think doing this check every time i add a log entry seems like a waste as its only an action that needs to be performed once a day yet i make 1000's of writes to the log file every day.

    Any suggestions on the best way to trigger the rotation? Is there a way to trigger the rotation when the windows system passes midnight?

    What triggers a new file being created?? Is it the start of a new day?? If it is then why not delete the file that's older than 7 days when you are creating the new file?? Seems simple to me!


  • Advertisement
  • Registered Users Posts: 7,501 ✭✭✭BrokenArrows


    nicol wrote: »
    What triggers a new file being created?? Is it the start of a new day?? If it is then why not delete the file that's older than 7 days when you are creating the new file?? Seems simple to me!

    Brilliant idea. Hadnt thought of doing that.

    Im currently just using an append with a datestamp so it automatically creates the new file when the date changes but i can easily just change it.

    Thanks


  • Registered Users Posts: 385 ✭✭nicol


    Brilliant idea. Hadnt thought of doing that.

    Im currently just using an append with a datestamp so it automatically creates the new file when the date changes but i can easily just change it.

    Thanks

    No bother, sometimes the best solutions are the simplest.


  • Registered Users Posts: 2,022 ✭✭✭Colonel Panic


    The best solution is using something like log4net! What he's got now is a workaround!


  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    Or you could write to the Windows Event Log and set your retention/rollover options in that.


  • Registered Users Posts: 385 ✭✭nicol


    The best solution is using something like log4net! What he's got now is a workaround!

    Or he could use the Logging Aplication Blocks from MS

    http://msdn.microsoft.com/en-us/library/ff632023.aspx

    There are a load of solutions to his problems, mine is the quickest and simplest to implement. Solution / Workaround / Call it what you like!! ;)


  • Advertisement
  • Registered Users Posts: 2,022 ✭✭✭Colonel Panic


    stevenmu wrote: »
    Or you could write to the Windows Event Log and set your retention/rollover options in that.

    For the services I've written, I don't write too much to the Windows event log beyond basic informational stuff, warnings and errors. Although in debug mode, I've got trace info going to the event log too.

    For the more verbose stuff, I'd still use some external logging!
    nicol wrote: »
    Or he could use the Logging Aplication Blocks from MS

    http://msdn.microsoft.com/en-us/library/ff632023.aspx

    There are a load of solutions to his problems, mine is the quickest and simplest to implement. Solution / Workaround / Call it what you like!! ;)

    Yep, there's plenty of ways to do it. I guess my point was "not invented here" syndrome is counter productive not that your way was crap!


  • Moderators, Society & Culture Moderators Posts: 9,689 Mod ✭✭✭✭stevenmu


    For the services I've written, I don't write too much to the Windows event log beyond basic informational stuff, warnings and errors. Although in debug mode, I've got trace info going to the event log too.

    For the more verbose stuff, I'd still use some external logging!
    That's a good point, it's not the best for very verbose logging.


  • Registered Users Posts: 7,501 ✭✭✭BrokenArrows


    For the services I've written, I don't write too much to the Windows event log beyond basic informational stuff, warnings and errors. Although in debug mode, I've got trace info going to the event log too.

    For the more verbose stuff, I'd still use some external logging!



    Yep, there's plenty of ways to do it. I guess my point was "not invented here" syndrome is counter productive not that your way was crap!

    It would just not be feasable to use a 3rd party tool to perform my logging.
    The logging is just a dump of information which needs to be rotated.

    I didnt need all the complexity that those tools provided.


  • Registered Users Posts: 2,021 ✭✭✭ChRoMe


    It would just not be feasable to use a 3rd party tool to perform my logging.
    The logging is just a dump of information which needs to be rotated.

    I didnt need all the complexity that those tools provided.

    Funnily enough, log4j/net would probably be simpler than whats being suggested here.


  • Registered Users Posts: 7,629 ✭✭✭Trampas


    Could you make an easy change to write to a db?


Advertisement