C# using a method from a class to another class, instance, DependencyProperty.Register error

73 Views Asked by At

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"; // 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 there is an example of where I would like to Log a message in the app but also export the message:

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 can't make it static because LogGrid and IsErrorLog return the 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 gave the instance the name of 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'.'

In summary I just want my method to export all the logs to a text file, which it makes a new txt every time you run it again. Also if it has a warning or error, it instead goes towards a txt file ErrorLogs.

I have read many forums and I still have no clue where to go.

0

There are 0 best solutions below