Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
113 lines (83 sloc) 3.9 KB

Creating a custom context provider

Context providers are used to automatically attach information to each error report. The information is useful to determine why an exception have been thrown.

To create a provider you need to create a class which implements the context provider interface. In the .NET standard library the interface is named IContextCollectionProvider and in .NET (4.x) it's named IContextInfoProvider.

.NET Standard example

Here is a sample for .NET Core MVC:

public class ViewBagProvider : IContextCollectionProvider
{
    /// <inheritdoc />
    public ContextCollectionDTO Collect(IErrorReporterContext context)
    {
        if (!(context is MvcCoreErrorContext aspNetContext) || aspNetContext.ViewBag == null)
            return null;

        var converter = new ObjectToContextCollectionConverter();
        return converter.Convert(Name, aspNetContext.ViewBag);
    }

    /// <inheritdoc />
    public string Name => "ViewBag";
}

.NET 4.x example

Here is an example from ASP.NET MVC5:

public class ViewBagProvider : IContextInfoProvider
{
    /// <inheritdoc />
    public ContextCollectionDTO Collect(IErrorReporterContext context)
    {
        var aspNetContext = context as AspNetMvcContext;
        if (aspNetContext?.ViewBag == null)
            return null;

        var converter = new ObjectToContextCollectionConverter();
        var collection = converter.Convert(Name, aspNetContext.ViewBag);

        //not beatiful, but we do not want to reflect the object twice
        return collection.Properties.Count == 0 ? null : collection;
    }

    /// <summary>ViewBag</summary>
    public string Name => "ViewBag";
}

Getting information from the .NET library

In most cases you need to be able to get information from your favorite .NET framework/library. For that Coderr provides a context that you can use to extract information.

The context is different for each Coderr client library. Here is a list of all available contexts:

Client name URL
Base library Github link
ASPNET Github link
ASPNET MVC5 Github link
WPF Github link
WinForms Github link

Conventions

A couple of conventions which can be used within your collector.

Exceptions during collection

A context info provider MUST not throw exceptions. Therefore you need to have a catch all which wraps your logic. You can however include the exception within your context collection to be able to analyze the exception in our UI.

var properties = new Dictionary<string,string>();
try
{
    properties["CurrentUser"] = Thread.CurrentPrincipal.Identity.Name;
}
catch (Exception ex)
{
    properties["CurrentUser.Error"] = ex.ToString();
}
return new ContextInfoCollection("YourCollectionName", properties);

Adding tags

You can add tags to error reports by using a context collection property named ErrTags:

var properties = new Dictionary<string,string>();
try
{
    properties["CurrentUser"] = Thread.CurrentPrincipal.Identity.Name;
    properties["ErrTags"] = "fatal";
}
catch (Exception ex)
{
    properties["CurrentUser.Error"] = ex.ToString();
}

return new ContextInfoCollection("YourCollectionName", properties);