简体   繁体   中英

How can various classes use only one instance of a logfile class

I'm working on C# script ConsoleApp with a few dll type projects added. Overview of the script is this- The script will basically connect to an external ActiveDirectory and do a search on a given OU. Get a list of all users from that OU and then connect via Microsoft Graph API and do 1) Invite the user for B2B and 2) Update some attributes on these invited users in AzureAD.

My solution has a few projects.

  1. ConsoleApp - (this is start of the program)
  2. LogicLayer - DLL. This is where the main logic resides. Such as which users are new in AD, which ones to remove from Azure etc.
  3. DALActiveDirectory - DLL. This is where script will connect to AD and return data back to LogicLayer.
  4. DALGraphAPI - DLL. This is where the script will connect to GraphAPI. Contains functions to get all users, get user by Id, update user attributes etc. Mainly methods here are called from LogicLayer.
  5. PasswordCrypto - Dll. contains function to retrieve encrypted text from a file and decrypt it to get the passwords. Both DALGraphAPI and DALActiveDirectory use respective passwords.
  6. LogFile - dll This is where I have defined a class to create a logfile with its name formatted with timestamp. eg "Log-yyyy_MM_dd_HH_mm_ss_ffff.txt".

Now I want to use LogFile instance to log certain things (such as errors, some debug messages) as control passes from ConsoleApp to LogicLayer to DALActiveDirectory to PasswordCrypto ...and so on. However, I only want one instance of this LogFile class. And no matter where the control is, the app should use that one instance to log to that one log file. If I do this in every class:

LogFile logFile = new LogFile();

then, it creates a new log file (with new name because of the timestamp). I want only one logfile created per full run of the console app. How can I achieve this? One way is to create one instance in ConsoleApp (which is the startup project) and then pass it other classes. I think it will work but is not very elegant solution. I've read something called Singleton pattern on internet but not sure if it applies to this.

Any suggestions?

Here is an example of a singleton pattern.


 Private Shared objLoggerTool As LoggerTool

    Public Shared Function GetLoggerToolInstance(ByVal strLogPath As String, ByVal iLogLevel As Integer) As LoggerTool
        If (objLoggerTool Is Nothing) Then
            objLoggerTool = New LoggerTool(strLogPath, iLogLevel)
        End If
        Return objLoggerTool
    End Function


you instantiate the logger as

        'instantiate the logger 
        _loggerTool = LoggerTool.GetLoggerToolInstance(config.LogFilePath, config.LogLevel)

This is a vb.net example from an app I did but it should help you understand how to create the logger and use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM