Nickolodeon
Nickolodeon

Reputation: 2956

write to log file class

I am just wondering on a good way to write to log.

I've created a singleton class Logger which has 2 methods: Write(eventDate, messageType, message) and Dispose().

In constructor I create StreamWriter (with AutoFlush set to true) which is used to append lines in a Write method. Logging is used extensively (but in one thread), about 10-15 records per second.

Now I reflect on the following two questions, comments on which I expect from you :)

First is autoflushing - does it open and close file all the time? Is File.AppendText similar?

Second, in Dispose I flush StreamWriter and then call Dispose on it. What if Dispose isn't called? How can I make sure it will be disposed of? Because if I remove AutoFlush and will flush the stream time to time (may be by maintaining inner counter) and when n records are written to buffer, I'd flush it, but what if less than n records are written and program terminates?

Upvotes: 3

Views: 4934

Answers (2)

Jon Erickson
Jon Erickson

Reputation: 114956

Is there any reason you can't use an open source logging framework such as NLog?

It's pretty easy to set up to write to a file

Here is a really easy way to write your first logs to a file:

  1. Right-click references
  2. Click on Manage NuGet References
  3. Make sure the "Online" tab on the left is selected, and search for "NLog"
  4. Install "NLog Schema for Intellisense(TM)" (this will automatically install an xml NLog.config and the xsd file for xml intellisense support and pull in the references needed for NLog) NLog Schema for Intellisense(TM)
  5. Open the automatically created NLog.config file and uncomment the sample target and rule, they should look like so:

    <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log"
            layout="${longdate} ${uppercase:${level}} ${message}" />
    

    <logger name="*" minlevel="Trace" writeTo="f" />
    

  6. Now you are all set up to log stuff, with the sample configuration whenever you log something it will append it to a {shortdate}.log file under the /logs directory where the executable is run, so if you did something like:
    var logger = NLog.LogManager.GetLogger("SampleLogger");
    logger.Trace("Logged Trace");
    logger.Debug("Logged Debug");
    logger.Info("Logged Info");
    logger.Warn("Logged Warn");
    logger.Error("Logged Error");
    logger.Fatal("Logged Fatal");
    

you'll get this log file

NLog File

Upvotes: 5

paulsm4
paulsm4

Reputation: 121869

  1. Open/code automatically "flushes" ... but Autoflush doesn't "close/open".

  2. In theory, you don't even need "Dispose" (this is managed code, after all). You do need to call "Close()" somewhere.

  3. If the program closes gracefully, it should automagically flush and close - all your data should be written.

    If the program terminates abnormally - all bets are off as to what will or won't happen. All you know is that your resources will be freed - but your data may or may not be written (and the data file may or may not be corrupted).

'Hope that helps...

Upvotes: 1

Related Questions