Enable Logging for Basis

Summary

The following are the steps I need to take to enable logging for Basis in the CEX worker.


EnvironmentFactory.cs - Turn on logging in Basis
public EnvironmentWrapper Create(string rootPath, string environmentXml)
{
    SetPath(rootPath);
    BasisTechnology.RLP.Environment.SetBTRootDirectory(rootPath);

    LogCallback logCallback = new LogCallback(MyLogCallBack);
    BasisTechnology.RLP.Environment.SetLogCallback(logCallback);
    BasisTechnology.RLP.Environment.SetLogLevel("all");

    var env = CreateEnvironment(environmentXml);
    return new EnvironmentWrapper(env);
}


EnvironmentFactory.cs - Create the function that Basis will callback to log
void MyLogCallBack(int channel, string message)
{
    RobLog("channel = " + channel + " message = " + message);
}


EnvironmentFactory.cs - Just my standard logging function to write to a text file
public void RobLog(string message)
{
    try
    {
        using (var mutex = new Mutex(false, "RobLogLock"))
        {
            var mutexAcquired = false;
            try
            {
                // acquire the mutex (or timeout after 60 seconds) will return false if it timed out
                mutexAcquired = mutex.WaitOne(60000);
            }
            catch (AbandonedMutexException)
            {
                // abandoned mutexes are still acquired, we just need to handle the exception and treat it as acquisition
                mutexAcquired = true;
            }

            // if it wasn't acquired, it timed out, so can handle that how ever we want
            if (!mutexAcquired)
            {
                throw new Exception("Unable to obtain a lock on the process to write to the roblog file");
            }

            // otherwise, we've acquired the mutex and should do what we need to do,
            // then ensure that we always release the mutex
            try
            {
                using (TextWriter tw = new StreamWriter(@"C:\temp\roblog.txt", true))
                {
                    // write a line of text to the file
                    tw.WriteLine(message);
                }
            }
            finally
            {
                mutex.ReleaseMutex();
            }
        }
    }
    catch { }
}


No comments:

Post a Comment

} else { }