Click here to Skip to main content
15,846,091 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
So I have a button made from my xaml file (view), called Export. When the user clicks on it, the logs created during the run of the app get exported to Logs.txt. If there are Warnings, Errors or Exceptions, those will instead be created in another txt file called ErrorLogs.txt

Here is my code now:

private bool IsErrorLog(string logText)
    return logText.Contains(LogType.Error.ToString()) || logText.Contains(LogType.Warning.ToString()) || logText.Contains(Exception);


private void btn_ExportLogs_Click(object sender, RoutedEventArgs e)
    string logsFolder = "LogsFolder"; // Folder for logs
    if (!Directory.Exists(logsFolder))

    string timestamp = DateTime.Now.ToString("hhmmss");
    string logFileName = Path.Combine(logsFolder, $"Logs_{timestamp}.txt");
    string errorLogFileName = Path.Combine(logsFolder, $"ErrorLog_{timestamp}.txt");

    using (StreamWriter logWriter = new StreamWriter(logFileName))

        foreach (var item in LogGrid.Children)

            logFileName = $"{item}";

            if (IsErrorLog(logFileName))
                using (StreamWriter errorLogWriter = new StreamWriter(errorLogFileName))


My issue here firstly is that I dont know how to add Exception in the isError method return. It says ""Exception" is a type, which is not valid in the given context"

I tried to make a second parameter Exception ex but then I will need to use that second parameter in the if in the btn_ExportLogs_Click method and I dont know what I should put there.

My second issue is that even with this code, all the logs go only in the Logs file, even if I try to make some errors in the app (like deleting something that doesnt exist anymore for example), it will anyways put it in the Logs file like all the other logs, and not create an ErrorLogs.txt and put it there.

What I have tried:

I tried putting the using inside the if, and it never creates cause all the logs are in Logs, and when i put the using just under the other using, it was always creating the txt file but always empty.

I tried to make a second parameter Exception ex but then I will need to use that second parameter in the if in the btn_ExportLogs_Click method and I dont know what I should put there.

I tried putting the "using" (errorLogWritter) inside the if, and it never creates the error file cause all the logs are in Logs.txt, and when i put the "using" just under the other "using" (logWritter), it was always creating the txt file but always empty due to the same reason.

In summary I expect that when I do random actions on the app, the logs that get created in the front page to be exported to a Logs.txt when i click on the export button (which means calling the method) but if there are LogType.Warning or/and LogType.Error or/and Exceptions to put them in a separate txt file called ErrorLogs.txt

I have other classes, but also a main classes where with each action it Logs a message, where at the end I say what is the LogType. I am expecting what I wrote to catch on that and differentiate between LogType.Info which is just normal, and the Warning & Error.

I hope i was clear, I am sorry if i'm not.

I tried reading tons of forums and documentations but I couldnt find something that helped me.
Updated 24-Nov-23 9:17am
Richard MacCutchan 24-Nov-23 3:48am    
Why not write the different messages to the normal or error logs as the application runs? 24-Nov-23 5:04am    
I would debug what the return value / string of

return logText.Contains(LogType.Error.ToString()) || logText.Contains(LogType.Warning.ToString()) || logText.Contains(Exception);

really is.
Perhaps it is a string of type and not a string of your expected value?

1 solution

The best solution, IMHO, would be to create a custom log provider. You can then handle the logging as the log entries are added.

I wrote an article for a custom Log Viewer control that has a custom Log Provider, one for each of the 4 common logging systems: Microsoft Logger, Serilog, NLog, & Log4Net. At the end of the article there are links to documentation for each of the loggers. The article and links should give you the information needed for you to create your own.

Here is a link to the article here on CodeProject: LogViewer Control for WinForms, WPF, and Avalonia in C# & VB[^]
Share this answer
Maciej Los 26-Nov-23 10:38am    

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