Вопрос

I'm facing a problem here, i'm in internship right now, and I have to modify a given program. I had some lag issue, and i finally found the problem : the log writer ...

The more the program was running, the more it was slow, and actually. I found out it was because of the streamwriter for the logfile, here is the code :

using (StreamWriter swLog = new StreamWriter(GenerateDaylog(_logPath), true))
{    
    String sLog = "";
    sLog += DateTime.Now.ToShortDateString();
    sLog += " ";
    sLog += DateTime.Now.ToLongTimeString();
    sLog += "\t";
    sLog += new StackFrame(1).GetMethod().DeclaringType.Name;
    sLog += "\t";
    sLog += new StackFrame(1).GetMethod().Name;
    sLog += "\t";
    sLog += msg;
    Trace.Listeners.Add(new TextWriterTraceListener(swLog));
    Trace.WriteLine(sLog);          
}

The log files are about 100 - 500 ko right now, every day a new logfile is created with the date in the name of it.

I guess there is a problem with the streamwriter, but I can't figure out what it is.

Это было полезно?

Решение

This is probably the issue:

Trace.Listeners.Add(new TextWriterTraceListener(swLog));

Every time you add a log entry, you're adding an extra trace listener... so the first time, you'll get one log entry. The second time, you'll get two (new) log entries, etc. You should be able to see this in your log (although as you're adding a listener and then disposing of the writer, it's not really clear what will happen).

It's not clear why you're using Trace.WriteLine at all given that you know exactly what you're trying to write to - why not just use swLog.WriteLine(...)?

Also, I'd encourage you to use string.Format instead of all that repeated string concatenation... and ideally use a dedicated logging framework (e.g. NLog or log4Net) instead.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top