Click here to Skip to main content
15,907,281 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
hello,
I am trying to write a C# web service program, and as you know, this program will never end until someone kill the process/service.

Now i need to add a log writer to this program to log normal-information/error/performance data, but I find that it is hard to decide when and where to close the streamwriter object for log:

First it is stupid to open/close streamwriter object for every line of log, because it will be very slow.
Then I think maybe I can create a thread to control a log buff pool, if the pool is more than 100 line of log, The thread will open the streamwriter, write all the logs, and then close it. It seems very good for program performance, but it will lost the most important logs! If an exception makes program crash, and all logs in buff pool will lost.
So i think maybe i can keep the streamwriter open all the time, and close it in Dispose() method of logwriter class? But who and how will this Dispose method been called?

I am not very clear about this problem, someone can help me?
Posted

wrote:
First it is stupid to open/close streamwriter object for every line of log, because it will be very slow.


Are you going to be logging non stop ? If not, then this is a dumb statement.

wrote:
So i think maybe i can keep the streamwriter open all the time, and close it in Dispose() method of logwriter class? But who and how will this Dispose method been called?


It won't. If I managed to get myself upset about the problem you describe, I'd just write everything right away, and perhaps use a timer to close the stream after a certain period of inactivity and then recreate it.
 
Share this answer
 
I have a reusable logging component I use for several different projects. Some programs write log entries several times a second and I see no significant lag due to the logging. If you do an asynchronous write, your main thread will run concurrently with the log operation. See the code below for an idea on how this can be done.

using System;
using System.Text;
using System.IO;
using System.Threading;
[...].
string m_FileName;
string m_Directory;
AutoResetEvent m_FileWriteComplete;
[...].
        /// <summary>
        /// Appends crlf and writes the string msg to the log file.  Closes the file when finished
        /// </summary>
        /// <param name="msg"></param>
        public void Write(string msg)
        {
            m_FileWriteComplete.WaitOne();
            FileStream fs = File.Open(Path.Combine(m_Directory, m_FileName), FileMode.Append,
                FileAccess.Write, FileShare.ReadWrite);
            
            msg = msg + "\r\n";                
            byte[] writeBuff = ASCIIEncoding.ASCII.GetBytes(msg);
            fs.BeginWrite(writeBuff, 0, writeBuff.Length, new AsyncCallback(WriteCallBack), fs);            
        }

        void WriteCallBack(IAsyncResult result)
        {
            FileStream fs = null;
            try
            {
                fs = (FileStream)result.AsyncState;
            }
            finally
            {
                if (fs != null)
                {
                    
                    fs.Close();
                    fs.Dispose();
                }
                m_FileWriteComplete.Set();
            }   
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900