"streamwriter"应该在哪里?对于日志已关闭? [英] where should a "streamwriter" for log been closed ?

查看:70
本文介绍了"streamwriter"应该在哪里?对于日志已关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我正在尝试编写C#Web服务程序,并且您知道,除非有人杀死该进程/服务,否则该程序将永远不会结束.

现在,我需要向该程序添加一个日志编写器以记录正常信息/错误/性能数据,但是我发现很难决定何时以及在何处关闭流编写器对象以记录日志:

首先,为每行日志打开/关闭streamwriter对象是很愚蠢的,因为这将非常慢.
然后,我想也许我可以创建一个线程来控制日志缓冲池,如果该池超过100行日志,则该线程将打开流写入器,写入所有日志,然后将其关闭.看起来对程序性能非常好,但是它将丢失最重要的日志!如果异常导致程序崩溃,并且buff池中的所有日志都将丢失.
因此,我想也许我可以一直保持streamwriter处于打开状态,并在logwriter类的Dispose()方法中将其关闭?但是谁会调用此Dispose方法,又将如何调用它?

我对这个问题不是很清楚,有人可以帮忙吗?

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?

推荐答案

写道:​​

首先,为每行日志打开/关闭streamwriter对象是很愚蠢的,因为这将非常慢.

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.

写道:​​

所以我想也许我可以一直保持streamwriter处于打开状态,并在logwriter class的Dispose()方法中将其关闭?但是谁将调用此Dispose方法?如何调用此方法?

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.


我有一个可重用的日志记录组件,用于几个不同的项目.有些程序每秒写几次日志条目,但由于日志记录,我看不到明显的延迟.如果执行异步写入,则主线程将与日志操作同时运行.请参阅下面的代码,以获取有关如何完成此操作的想法.

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();
            }   
        }


这篇关于"streamwriter"应该在哪里?对于日志已关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆