Loosing items

Nov 17, 2016 at 9:26 PM
When I use TraceLogFileWriter it seems to loose the last traced items. The TraceLogFileWriter is created with a using statement, then I trace 50 items and exit loop using Environment.Exit(0). The log file only contains 47 items
Nov 18, 2016 at 3:57 AM
Thanks for your feedback. Hm, I didn't know Environment.Exit(0) existis and it might not be the best way to end your program.

From MSDN:

"Exit terminates an application immediately, even if other threads are running. "
The TraceLogFileWriter is running on another thread. That would explain why not everything gets written.

"If Exit is called from a try or catch block, the code in any finally block does not execute."
I guess based on this, one has to assume that also the Dispose() from the using statement doesn't get called after Exit is called. No wonder, when then some traces get lost.

I recommend to exit your program properly. If you have to use Environment.Exit(0), then dispose the TraceLogFileWriter explicitly first.
Nov 23, 2016 at 2:57 PM
I created a console program, I even put TraceLogWriter in a using statement so that it is disposed. But when I run this, not all log lines are in the trace file.
(Without using statement even less get's logged)

When I put console.readkey at the end , and wait a second, than all lines are in the log file.

Is there a specific call needed to ensure all lines are logged?

Also, is there a reason why you target .net 4.6.1 ? It limits it's usage severally, not all projects can be converted to the lastest .net version? Would you conside releasing a DLL for eq. .net 4.5 ?
        private static TraceLogFileWriter logWriter;
        static void Main(string[] args)
            Tracer.IsTracing = true;
            Tracer.IsWarningTracing = true;
            Tracer.IsErrorTracing = true;
            Tracer.IsExceptionTracing = true;
            Tracer.IsBreakOnWarning = false;
            Tracer.IsBreakOnError = true;
            Tracer.IsBreakOnException = true;

            using (logWriter = new TraceLogFileWriter(
                    directoryPath: Environment.CurrentDirectory + @"\LogFiles",
                    newFileName: "testlog",
                    maxFileByteCount: 10 * 1000 * 1000,
                    maxFileCount: 5,
                    logFileWriterTimerInitialDelay: 1000, //msec 
                    logFileWriterTimerInterval: 10 * 1000))
                    Tracer.TraceLine("Start logging");
                    Enumerable.Range(1, 50).ToList().ForEach(n =>
                        Console.WriteLine($"message {n}");
                        Tracer.TraceLine($"message {n}");
                        if (n > 30)
                            throw new Exception("An error occurred.");
                catch (Exception ex)
                Console.WriteLine("Ending logging");
                Tracer.TraceLine("Ending logging");
Jan 31 at 5:11 PM
Sorry for my slow reply. I am new to Codeplex and I saw your discussion and issue only now.

You are right that the library should use a lower Framework version. I changed it to 4.0.

I still need to investigate why lines get lost when you close the writer, the code is written so that in Dispose() everything is written to the file, including flush and close.
Feb 16 at 10:49 AM
The problem was that Tracer stores all messages very quickly first in a temporary buffer and then copies them every 100 millisecond using another thread to the final message buffer. If a trace is written and then the program stopped, the timer did not run and the trace message were not copied yet. I addressed this problem in version 1.1 with introducing of Tracer.Flush(), which moves all temporarily stored messages to the permanent buffer. GetTrace() now calls Flush() per default, unless the parameter needsFlushing is false.
Flush() gets also called before disposing a TraceLogFileWriter.
Marked as answer by PeterHuberSg on 2/16/2017 at 2:49 AM