Click here to Skip to main content
11,720,545 members (71,054 online)
Click here to Skip to main content
Articles » Web Development » ASP.NET » General » Downloads
Add your own
alternative version

MVC Basic Site: Step 2 - Exceptions Management

, 25 Oct 2013 Ms-PL 68.8K 4.6K 130
This second article from the "MVC Basic Site" series presents in details the exceptions management rules and their implementation for an ASP.NET MVC web site, and provides some utile base classes and source code for Logging and Exceptions Management that can be reused.
MvcBasicSiteDatabase.zip
MvcBasicSiteDatabase.bak
MvcBasicSiteSourceCode.zip
MvcBasicSiteSourceCode
MvcBasicSite
CreateEventLogEntry
Properties
MvcBasic.Logic
ClassDiagram1.cd
MvcBasicSiteModel.edmx
Properties
MvcBasicSite
app_code
App_GlobalResources
ClassDiagramUI.cd
Content
Images
dd_arrow.gif
flag_De.png
flag_En.png
flag_Ro.png
HeaderLogo.png
icon-arrow.gif
themes
base
images
ui-bg_flat_0_aaaaaa_40x100.png
ui-bg_flat_75_ffffff_40x100.png
ui-bg_glass_55_fbf9ee_1x400.png
ui-bg_glass_65_ffffff_1x400.png
ui-bg_glass_75_dadada_1x400.png
ui-bg_glass_75_e6e6e6_1x400.png
ui-bg_glass_95_fef1ec_1x400.png
ui-bg_highlight-soft_75_cccccc_1x100.png
ui-icons_222222_256x240.png
ui-icons_2e83ff_256x240.png
ui-icons_454545_256x240.png
ui-icons_888888_256x240.png
ui-icons_cd0a0a_256x240.png
Controllers
Global.asax
Models
MvcBasicSite.csproj.user
Properties
Scripts
Views
Account
Home
Shared
MvcBasicSiteSourceCodeMVC4.zip
ClassDiagram1.cd
MvcBasicSiteModel.edmx
App_Data
ClassDiagramUI.cd
dd_arrow.gif
flag_De.png
flag_En.png
flag_Ro.png
HeaderLogo.png
icon-arrow.gif
animated-overlay.gif
ui-bg_flat_0_aaaaaa_40x100.png
ui-bg_flat_75_ffffff_40x100.png
ui-bg_glass_55_fbf9ee_1x400.png
ui-bg_glass_65_ffffff_1x400.png
ui-bg_glass_75_dadada_1x400.png
ui-bg_glass_75_e6e6e6_1x400.png
ui-bg_glass_95_fef1ec_1x400.png
ui-bg_highlight-soft_75_cccccc_1x100.png
ui-icons_222222_256x240.png
ui-icons_2e83ff_256x240.png
ui-icons_454545_256x240.png
ui-icons_888888_256x240.png
ui-icons_cd0a0a_256x240.png
minified
images
animated-overlay.gif
ui-bg_flat_0_aaaaaa_40x100.png
ui-bg_flat_75_ffffff_40x100.png
ui-bg_glass_55_fbf9ee_1x400.png
ui-bg_glass_65_ffffff_1x400.png
ui-bg_glass_75_dadada_1x400.png
ui-bg_glass_75_e6e6e6_1x400.png
ui-bg_glass_95_fef1ec_1x400.png
ui-bg_highlight-soft_75_cccccc_1x100.png
ui-icons_222222_256x240.png
ui-icons_2e83ff_256x240.png
ui-icons_454545_256x240.png
ui-icons_888888_256x240.png
ui-icons_cd0a0a_256x240.png
Global.asax
MvcBasicSite.csproj.user
#region Copyright (c) 2013 Raul Iloc
/*
{***************************************************************************}
{                                                                           }
{       Copyright (c) 2010  RAUL ILOC (rauliloc@yahoo.com)                  }
{       ALL RIGHTS RESERVED                                                 }
{                                                                           }
{   THE WORK IS PROVIDED UNDER THE TERMS OF THIS CODE PROJECT OPEN LICENSE. }
{   THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.         }
{   ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE         }
{   OR COPYRIGHT LAW IS PROHIBITED.                                         }
{                                                                           }
{                                                                           }
{  BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HEREIN, YOU ACCEPT AND     }
{  AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. THE AUTHOR GRANTS YOU    }
{  THE RIGHTS CONTAINED HEREIN IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH  }
{  TERMS AND CONDITIONS. IF YOU DO NOT AGREE TO ACCEPT AND BE BOUND BY THE  }
{  TERMS OF THIS LICENSE, YOU CANNOT MAKE ANY USE OF THE WORK.              }
{                                                                           }
{***************************************************************************}
*/
#endregion Copyright (c) 2013 Raul Iloc
using System;
using System.Diagnostics;

namespace MvcBasic.Logic
{
    /// <summary>
    /// Defines the class used for write messages and exceptions information into the Windows event log. 
    /// </summary>
    public class MvcBasicLog
    {
        /// <summary>
        /// The even log source name used by "MVC Basic" system.
        /// </summary>
        private static readonly string _logSource = "MVC Basic";

        /// <summary>
        /// Write a message into the event log.
        /// </summary>
        /// <param name="message">The message.</param>
        public static void LogMessage(string message)
        {
            LogMessage(null, message);
        }

        /// <summary>
        /// Write a message into the event log.
        /// </summary>
        /// <param name="category">The category (like a project name).</param>
        /// <param name="message">The message.</param>
        public static void LogMessage(string category, string message)
        {
            if (category == null)
                AddLogLine(message, false);
            else
                AddLogLine(string.Format("[{0}] : {1}", category, message), false);
        }

        /// <summary>
        /// Write an error message into the event log.
        /// </summary>
        /// <param name="message">The message.</param>
        public static void LogError(string message)
        {
            LogError(null, message);
        }

        /// <summary>
        /// Write an error message into the event log.
        /// </summary>
        /// <param name="message">The message.</param>
        public static void LogError(string category, string message)
        {
            if (category == null)
                AddLogLine(message, true);
            else
                AddLogLine(string.Format("[{0}] : {1}", category, message), true);
        }

        /// <summary>
        /// Write the entire exception string into the event log.
        /// </summary>
        /// <param name="ex">The exception.</param>
        public static void LogException(Exception ex)
        {
            LogException(null, ex);
        }

        /// <summary>
        /// Write the entire exception string into the event log.
        /// </summary>
        /// <param name="category">The category (like a project name).</param>
        /// <param name="ex">The exception.</param>
        public static void LogException(string category, Exception ex)
        {
            if (category == null)
            {
                AddLogLine(ex.ToString(), true);
            }
            else
            {
                AddLogLine(string.Format("[{0}] : {1}", category, ex.ToString()), true);
            }
        }

        /// <summary>
        /// Write a new line with the given message into the event log.
        /// </summary>
        /// <param name="logMessage">The log message.</param>
        /// <param name="isError">True for an error message.</param>
        private static void AddLogLine(string logMessage, bool isError)
        {
            EventLog log = new EventLog();
            log.Source = _logSource;
            //
            try
            {
                log.WriteEntry(logMessage, (isError ? EventLogEntryType.Error : EventLogEntryType.Information));
            }
            catch (System.Security.SecurityException ex)
            {
                //
                // In Web app you do not have right to create event log source and
                // the log source must be created first by using the provided CreateEventLogEntry project!
                //
                throw new ApplicationException("You must create the event log entry for our source by using \"CreateEventLogEntry\" project!", ex);
            }
            catch
            {
                //
                // The log file is to large, so clear it first.
                //
                log.Clear();
                log.WriteEntry(logMessage, (isError ? EventLogEntryType.Error : EventLogEntryType.Information));
            }
            //
            log.Close();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

Raul Iloc
Program Manager SOFTVISION
Romania Romania
I am Project Manager with about 20 years working experience in software development and about 15 years as Project Manager and Architect.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150901.1 | Last Updated 25 Oct 2013
Article Copyright 2013 by Raul Iloc
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid