[How to] Log into a file instead of the Error Log
Logging with OneStream is great but when we all use the Error Log at the same time, things can get messy very quickly. Moreover, logging in a file instead of the Error Log can be very convenient when logging kickouts.
The initial setup can be a little involving but once you get a knack out of it, logging in a file is as easy as using the Error Log.
In this post, I will show you how to do the initial setup and then how to log into a file instead of the Error Log only when there is an exception or every time.
This rule and methodology is the result of the genius work of Matt Ha and I am very thankful he shared it with us.
Initial Setup
In order to log into a file, you need to import a Public Business Rule, it will be called by the logger and it is available in this post (GS_GlobalHelper.xml).
Side note, remember that in order to make a BR Public, you need to change the setting for ‘Contains Global Functions for Formulas’ to True.
Setup of the BR to log into a file
In order to call a Public function, you need to Reference it in your Business Rule
You also need to add it to your Imports:
Imports OneStream.BusinessRule.Finance.GS_GlobalHelper.MainClass
The Logger function needs 2 variables to be declared and set, I like to do it at the very top of my rule
#Region "Logging Variables"
Dim bVerboseLogging As String = True
Dim logger As New Text.StringBuilder
#End Region
Then, you need to add 2 Private Functions to your rule, this is where you set your naming convention (if you want to add the data and time or anything else) and the folder name where you want your files to be stored.
Private Sub StoreLoggerOnSession(ByVal si As SessionInfo, ByVal api As FinanceRulesApi, ByRef globals As BRGlobals)
' Multithread-safe way to store logger to session
Dim globalLogger As Text.StringBuilder = globals.GetObject("globalLogger")
If globalLogger Is Nothing Then
globals.SetObject("globalLogger", logger)
Else
globalLogger.AppendLine(logger.ToString)
End If
End Sub
Private Sub WriteSessionLogToFileShare(ByVal si As SessionInfo, ByVal api As FinanceRulesApi, ByRef globals As BRGlobals, ByVal logName As String)
' Write logger stored on session to application database
Dim globalLogger As Text.StringBuilder = globals.GetObject("globalLogger")
If globalLogger IsNot Nothing Then
WriteLogger(si, api, globalLogger, logName, "TestLogs", DateTime.Now.ToString("yyyy-MM-dd-hh-mm"))
End If
End Sub
In the end, your rule should look like this:
Logging into a file when there is an Exception
You can write to a file instead of the Error log when an Exception happens, in this case, update the Exception Catcher at the end of your Main Function.
Catch ex As Exception
logger.AppendLine(ex.Message)
If bVerboseLogging Then StoreLoggerOnSession(si, api, globals)
WriteSessionLogToFileShare(si, api, globals, "MyRule")
Throw ErrorHandler.LogWrite(si, New XFException(si, ex))
End Try
Logging anything into a file
Of course, you can also log anything you would usually log to the Error Log in a file instead. Use the “logger” instead of the Error Log.
'Log to the Error Log
api.LogMessage("Logging to the Error Log with the api")
brapi.ErrorLog.LogMessage(si, "Logging to the Error Log with the brapi")
'Log to a file
logger.AppendLine("Logging to a file").AppendLine
#Region "Push logs to the File Explorer"
If bVerboseLogging Then StoreLoggerOnSession(si, api, globals)
WriteSessionLogToFileShare(si, api, globals, "MyRule")
#End Region
Results
This is what you get in the Error Log
And this is what you get in the File Explorer: