MSI
MSI

Reputation: 1134

Umbraco CMS(.NET): Logging errors loading xslt/User controls

I was wondering if there's a way in Umbraco to log errors that we get when it fails to load xslt or user-controls. Generally it shows a red box saying it couldn't load the control and stuff. Is there a way to properly log this?

Thanks in advance.

Upvotes: 5

Views: 1128

Answers (1)

Elijah Glover
Elijah Glover

Reputation: 1976

First off, it's not really supported... When errors occur it outputs html and writes to the asp.net trace log.

Heres how I would approach this. Most of my Umbraco installations use Elmah for exception logging and log4net for application logging. This should give you any errors on output.

using System;
using System.Linq;
using System.Web;

public class MacroLogging : IHttpModule {

    public void Init(HttpApplication context) {
        context.LogRequest += ContextLogRequest;
    }

    static void ContextLogRequest(object source, EventArgs e) {
        var app = (HttpApplication)source;
        var context = app.Context;
        context.Trace.TraceFinished += TraceFinished;
    }

    static void TraceFinished(object sender, TraceContextEventArgs e) {
        var records = e.TraceRecords.Cast<TraceContextRecord>();
        var categoryTypes = new[] {"Macro", "macro", "umbracoMacro"};
        var traceOutput = records.Where(p => categoryTypes.Contains(p.Category) && p.IsWarning)));
        foreach (var entry in traceOutput) {
            //Your Output entry.Message
        }
    }

    public void Dispose() {}

}

Just add the module to your web.config. I have't tested as it's 1am :) but the overall concept should work.

Upvotes: 4

Related Questions