Top

Tags


Roadkill .NET Wiki

Google ads

Recommended reading


Search

Entries in asp.net (7)

Monday
Feb132012

Filtering System.Diagnostics Trace messages

One of the biggest points earners for me on Stackoverflow has been this question about Error logging in C#/.NET. Having been fairly experienced with the setup of Log4net, I've seen firsthand how much over kill it generally is for logging (unless you like to use your live servers for debugging), and also how it just isn't needed as .NET has its own in built and comprehensive logging framework built in.

One thing that isn't mentioned in my answer is how to filter your log messages according to error type: information, error, critical etc. It's fairly easy to do with TraceListeners, but requires quite a few extra lines of XML in your web.config/app.config. The example below shows how to filter information messages, it's setup to use the eventlog and write to the ASP.NET category, which is necessary with web apps if the user you are running the app pool with doesn't have admin access, or the registry key for a new event log source doesn't exist.

<system.diagnostics>
    <!-- Add multiple sources for varying levels of tracing -->
    <sources>
        <source name="debugSource" switchName="defaultSwitch" switchType="System.Diagnostics.SourceSwitch">
            <listeners>
                <add name="eventLogListener" type="System.Diagnostics.EventLogTraceListener" initializeData="ASP.NET 2.0.50727.0" />
            </listeners>
        </source>
    </sources>
    <switches>
        <add name="defaultSwitch" value="Error"/><!-- See System.Diagnostics.SourceLevels enum -->
    </switches>

    <trace autoflush="false" indentsize="4">
        <listeners>
            <add name="eventLogListener" type="System.Diagnostics.EventLogTraceListener" initializeData="ASP.NET 2.0.50727.0" />
        </listeners>
    </trace>
</system.diagnostics>

You should be using a wrapper/Façade class for the logging so you can have a single entry point and class with one responsibility in your app. Checking for the output level is then easy:

private TraceSource _traceSource  = new TraceSource("defaultSource");

public void LogInformation(string message,object params args)
{
    if ((_traceSource.Switch.Level & SourceLevels.Information) == SourceLevels.Information)
    {
    ...
    }
}
Sunday
Nov202011

403 Forbidden errors with ASP.NET MVC 3 

Have you just created an ASP.NET MVC 3 site on your local/developer box to use Windows authentication, mapped it correctly and then continually got an authentication box appearing even though you're typing the username/password correctly? And the permissions on disk are fine, you even have everyone mapped to the root folder.

When you cancel, you get a 403.1 or 403.x error. As I discovered this error comes from a security feature of Windows Vista/7 that stops loop back auth checks which essentially cripples your windows authentication settings without changing the registry. It only ever happens when you're testing the site on localhost, or using a domain that's mapped to 127.0.0.1 in your hosts file or DNS server.

There's a support.microsoft.com page about it here, but the most reliable fix is the 1st of the two options. Here's the relevant parts:

In Registry Editor, locate and then click the following registry key: HKEYLOCALMACHINE\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0

Right-click MSV1_0, point to New, and then click Multi-String Value.

Type BackConnectionHostNames, and then press ENTER.

Right-click BackConnectionHostNames, and then click Modify.

In the Value data box, type the host name or the host names for the sites that are on the local computer, and then click OK.

Monday
Apr042011

Listing all mime types without using OLE and IIS in .NET

An ASP.NET example of mapping file extensions to their mime types, without needing to query IIS via COM.

Click to read more ...

Wednesday
Mar022011

The new Spruce solution structure

I’m quite anally retentive when it comes to folder structure in Visual Studio projects: I like to keep all C# source and site assets in a structured way that will avoid me having to spend (accumulated) hours doing pointless searches and treasure hunts through the solution explorer...

Click to read more ...

Wednesday
Mar242010

ASP.NET control event order/lifecycle

A very concise reference for the order which events are fired when you subclass a control

Click to read more ...

Friday
Jan012010

Using JQuery with ASP.NET

This post is all about calling and parsing data from an ASP.NET web service using jQuery.

Click to read more ...

Monday
Jul132009

GZip and Deflate page compression in ASP.NET

A re-packaged version of Bart De Smet's ASP.NET page compression using GZIP.

Click to read more ...