Search code examples
.netenterprise-library

what contents are represented by the LogicalOperationStack TraceOutputOptions value


In the microsoft enterprise library 5, i am using the logging feature, when using this, i came across the "LogicalOperationStack" traceoption value. When i googled for this, i got this link: http://msdn.microsoft.com/en-us/library/system.diagnostics.traceeventcache.logicaloperationstack.aspx

But the contents presented in this link seems jargon for me. Can any one let me know the bottom line of what this is and what data do we get from this. Kindly explain me keeping in mind i am new to .net.

Thanks


Solution

  • The key thing to look at is the CorrelationManager.

    Traces generated from a single logical operation can be tagged with an operation-unique identity, in order to distinguish them from traces from a different logical operation.

    Logical operations can also be nested. The LogicalOperationStack property exposes the stack of nested logical operation identities. Each call to the StartLogicalOperation method pushes a new logical operation identity onto the stack. Each call to the StopLogicalOperation method pops a logical operation identity off the stack.

    So basically, the CorrelationManager let's you assign information to the thread. This information can be used to correlate the information in the log files. Since the information is on the thread it is available to be logged in any method.

    OK, so I don't think I've dispelled the jargon yet. :)

    Here's an example:

    class Program
    {
        static void Main(string[] args)
        {
            Trace.CorrelationManager.StartLogicalOperation(Guid.CreateGuid());
            Trace.CorrelationManager.StartLogicalOperation("TransferMoney");
    
            TransferMoney();
    
            Trace.CorrelationManager.StopLogicalOperation();
            Trace.CorrelationManager.StopLogicalOperation();
        }
    
        static void TransferMoney()
        {
            DebitAccount();
            WireMoney();
        }
    }
    

    Now if the methods DebitAccount and WireMoney log messages then the Guid (AKA Activity ID) and the logical name of the operation will be logged along with the rest of the message.

    This could let you trace a single request through all of your system layers by tracking the Activity ID. You could also use the logical name to analyze your logs to see usage patterns and performance from a logical perspective.

    On to Enterprise Library. Enterprise Library will add the LogicalOperationStack values to the log output (if configured):

    LogicalOperationStack=TransferMoney, b63e2f03-5433-40a2-9de5-6232d3aa7f68

    as well as adding each LogicalOperationStack value to the category list:

    Category: General, TransferMoney

    Note that the TraceOutputOptions are not output to the EventLog TraceListener. This can drive you crazy if you are trying to figure out why they don't show up. :)

    Enterprise Library already has the Tracer class that provides equivalent functionality to the code above (plus the ability to log trace messages with timings etc.). So the above code using Tracer would be:

    class Program
    {
        static void Main(string[] args)
        {
            using (Tracer tracer = new Tracer("TransferMoney", Guid.NewGuid()))
            {
                TransferMoney();
            }            
        }
    
        static void TransferMoney()
        {
            DebitAccount();
            WireMoney();
        }
    }