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