Join 34,000+ subscribers and receive articles from our
blog about software quality, testing, QA and security.

Some questions on getting started


#1

Firstly I would like to just say that this product is great. Really really powerful.

Also, it is good I don’t need to sign up to post a message, which is annoying.

Anyway, my questions:

-I have set some properties in the constructor of my logging class:

        SiAuto.Si.Enabled = true;
        SiAuto.Si.Connections = "tcp()";

This class (Si) has a Dispose() method. Where would I call this? Would I need a destructor?

Does this mean that all of the methods in the class will use these properties? Eg:

    void Si_Error(object sender, ErrorEventArgs e)
    {
        SiAuto.Main.LogException(e.Exception.Message, e.Exception);
    }

I am not sure as this event’s body is pointing to SiAuto.Main…

-With the event I have above, if I want to call it from my business logic, how best would I do it?

In your documentation (which is pretty comprehensive), you have:

public static void Main(string[] args)
{
// Register our event handler for the error event.
SiAuto.Si.Error += new ErrorEventHandler(EventHandler);
}

Does that mean that when there is an exception, this code is fired? Do I no longer need catch blocks in my business logic?

Thanks


#2

Thank you!

If you wrap SmartInspect in your own logger class, it’s probably best to add a Dispose method to your logger and call Si.Dispose from there. Calling Dispose on another managed object is usually not recommend in an object finalizer/destructor.

The question where you should then call Dispose of your logger depends on your application and how you use the logger. If you are writing an ASP.NET application, it makes sense to initialize the logger on Application Start and dispose it on Application Stop. WinForms and service applications are similar.

[quote] Does this mean that all of the methods in the class will use these properties? Eg:
I am not sure as this event’s body is pointing to SiAuto.Main…[/quote]

You generally do not need to use the SiAuto class. It’s just a static convenience class which provides some pre-instantiated objects. You could also create a custom SmartInspect object and custom SmartInspect sessions and use these in your own logger or everywhere else in your application.

[quote]void Si_Error(object sender, ErrorEventArgs e)
{
SiAuto.Main.LogException(e.Exception.Message, e.Exception);
}

Does that mean that when there is an exception, this code is fired? Do I no longer need catch blocks in my business logic?[/quote]

Si.Error is just for reporting exceptions which occur inside the SmartInspect logger (such as connection problems, e.g. when the Console is not available or something) and does not affect your actual application or business logic. Please note that it is not recommended to use SmartInspect in this event handler (as in your example) because this can lead to recursive calls to the error event handler (in case LogException internally throws an exception because of connection problems with the Console, for example).

In case you want to log all occurred exceptions of your application, you could combine SmartInspect with an aspect-oriented programming tool (AOP). I’ve recently written a blog post on how to use PostSharp (an AOP tool) with SmartInspect to implement this very scenario:

http://blog.gurock.com/?p=848

Hope that helps!

Regards,
Tobias