Click here to Skip to main content
15,847,427 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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.


C#
public void exportLogs ()
{

    string logsFolder = "CertificationTools_App\\bin\\Debug\\net48LogsFolder"; // 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))
    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:


C#
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.


C#
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.
Posted
Updated 23-Nov-23 18:55pm
v2
Comments
Andre Oosthuizen 24-Nov-23 14:22pm    
If the answer is not to your liking, please do not repost the same question again, the answer will remain the same...

1 solution

See my comment to your duplicate of this question.
 
Share this answer
 

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