Chat Support
Loupe - Log - Monitor - Resolve
Loupe / Developer's Guide / For .NET Framework / Logging / Developer's Guide - Logging - Using with Trace
In This Topic
    Developer's Guide - Logging - Using with Trace
    In This Topic

    The Loupe Agent is designed to work with the built-in Trace and Debug logging facilities.  Because these are built into every version of the .NET framework, the Agent will automatically work with Trace by default. 

    How to use Loupe with Trace

    To work with Trace, you need to:

    1. Have Trace log statements in your code.  At the end of the day, there has to be a call to either System.Diagnostics.Trace or System.Diagnostics.Debug to have any information show up from Trace logging.  For more information on how to use Trace, see below.
    2. Have the Loupe Agent register as a Trace Listener.  This will happen automatically whenever the Loupe Agent loads.  If you aren't otherwise calling the Loupe Agent and want to use it as a trace listener, it can be explicitly registered like any Trace listener.  See Getting Started - Adding Loupe to your Application .

    Once you have Loupe working with Trace, you can maximize the value you get out of Loupe by reviewing Logging - Optimizing Trace Logging.

    Adding Trace Logging to your code

    The built-in Trace and Debug logging capabilities of .NET are a great way to get started with logging.  They provide good basic capabilities and are always available because they're built into every version of the .NET runtime.  If no trace listeners are registered, the overhead of having Trace calls in your application is trivial.

    Example Trace Logging on Application Startup
    Copy Code
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Windows.Forms;
    namespace ExampleApplication
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Trace.TraceInformation("Application starting");
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new frmMain());
                Trace.TraceInformation("Application exiting normally");
                //When you're all done with Trace, it's a good idea to call close to have
                //all trace listeners flush and shut down gracefully.  This helps the
                //Loupe Agent know that your application didn't crash.
                Trace.Close();
            }
        }
    }
    
    ASP.NET doesn't use the normal Trace capabilities by default, but they're still there.  If you use Trace.Trace in ASP.NET you'll see a simplified version designed specifically for ASP.NET.  You can still use the normal Trace capability in System.Diagnostics.Trace.  For ASP.NET you can also register the Loupe Agent for ASP.NET which will automatically record additional trace logging for web applications.

    Conditional Logging

    The .NET Diagnostics classes include the Debug class which can take actions only when an application is compiled in Debug mode.  This is particularly useful when you want to include extra messages that would "spam" the log in normal use or include sensitive information (like passwords) that should not be logged.

    Debug Usage Example
    Copy Code
    private void ExecuteNonQuery(DbCommand command)
    {
        Debug.Assert(command != null);
        Debug.WriteLine("Starting to Execute Db Command");
        if (command.Connection.State != ConnectionState.Open)
        {
            Debug.WriteLine("Command Connection isn't open - opening.");
            command.Connection.Open();
        }
        Debug.WriteLine(string.Format("Executing Db Command On connection '{0}':", command.Connection.ConnectionString));
        Debug.WriteLine(command.CommandText);
        command.ExecuteNonQuery();
        Debug.WriteLine("Db Command Execution Complete");
    }
    

    Nothing done by the Debug class will happen if the project is compiled in Release mode.  In the above example (which is somewhat excessive even for debug use) none of the statements that start Debug would produce any execution code in a Release build, ensuring that there is no overhead to any work they do. 

    See Also