0
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))
{
Directory.CreateDirectory(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}";
logWriter.WriteLine(logFileName);
if (IsErrorLog(logFileName))
{
using (StreamWriter errorLogWriter = new StreamWriter(errorLogFileName))
errorLogWriter.WriteLine(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.
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.