I'm having some issues here. So I wrote this method in a class named MainWindow, that exports logs of an app to a txt. Honestly I dont know if its good but anyways we will see.
public void exportLogs ()
{
string logsFolder = "CertificationTools_App\\bin\\Debug\\net48LogsFolder";
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))
using (StreamWriter errorLogWriter = new StreamWriter(errorLogFileName))
{
foreach (var item in LogGrid.Children)
{
logFileName = $"{item}";
logWriter.WriteLine(logFileName);
if (IsErrorLog(logFileName))
{
errorLogWriter.WriteLine(errorLogFileName);
}
}
}
}
private bool IsErrorLog(string logText)
{
return logText.Contains(LogType.Error.ToString()) || logText.Contains(LogType.Warning.ToString());
}
In a class called SDKService here is an example of where I would like to Log message in the app but also export it:
public void DeleteCredentials(int nbOfCredentialsToDelete, bool deleteAll, bool deleteMatchingCardholders)
{
if (ListCredentials.Count == 0)
{
Log("No credentials to delete", LogType.Warning);
return;
}
if (ListCredentials.Count < nbOfCredentialsToDelete)
{
Log($"Only {ListCredentials.Count} credential(s) in the system - Impossible to delete {nbOfCredentialsToDelete}", LogType.Warning);
return;
}
... etc
I cant make it static cause LogGrid and IsErrorLog gets as error "an object reference is required for the non static field, method or property "MainWindow.LogGrid""
I made an instance of MainWindow in the SDKService class (at the beginning) and called the instance after the Log message line.
MainWindow export = new MainWindow();
...
public void DeleteCredentials(int nbOfCredentialsToDelete, bool deleteAll, bool deleteMatchingCardholders)
{
if (ListCredentials.Count == 0)
{
export.exportLogs();
Log("No credentials to delete", LogType.Warning);
return;
}
if (ListCredentials.Count < nbOfCredentialsToDelete)
{
export.exportLogs();
Log($"Only {ListCredentials.Count} credential(s) in the system - Impossible to delete {nbOfCredentialsToDelete}" , LogType.Warning);
return;
}
{
export.exportLogs();
Log("Starting delete", LogType.Info);
}
...
However when I run it, i get this error in the MainWindow class:
Method: public readonly DependencyProperty IsUserConnectedProperty = DependencyProperty.Register("IsUserConnected", typeof(bool), typeof(MainWindow), new PropertyMetadata(default(bool)));
Error Message: System.ArgumentException: ''IsUserConnected' property was already registered by 'MainWindow'.'
I am sorry if I didn't explain well or if I did something wrong. Any help would be appreciated.
What I have tried:
I read forums and I still have no clue where to go.
In summary I just want my method to export all the logs to a text file, which it makes a new txt everytime you run it again. Also if it has a warning or error, it instead goes towards a txt file ErrorLogs.