Link to home
Start Free TrialLog in
Avatar of enrique_aeo
enrique_aeo

asked on

error handling

Hi experts, I have finished developing a form of 30 questions.
I would like your help with the implementation of error handling, as it is a single form should be something simple that I can handle errors properly and avoid showing any error framework

I understand I should use try catch, but I need to implement its recommendations
Avatar of lenordiste
lenordiste
Flag of France image

your best bet is to use global.asax to manage any error that might occur on your site (in this case a single page). You can also simply provide a custom page in case of errors by modifying your web.config.

here is a starting point:
http://www.15seconds.com/issue/030102.htm

I personally would not use try/catch scenario on a small form unless you have specific error messages or mechanics you want to implement for that particular page.
Avatar of LlamaJoe
LlamaJoe

also, be sure to look at the asp.net validation controls - this will handle most of your "data entry" error catching - then use the global asax approach for logic and programming errors.

http://www.w3schools.com/aspnet/aspnet_refvalidationcontrols.asp
A form? Windows form you mean? Try this in Program.cs


Log is a logger of my own.

Best regards
In Main()

Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Application.ThreadException += Application_ThreadException;

private static void Application_ApplicationExit(object sender, EventArgs e){
            Application.ThreadException -= Application_ThreadException;
        }

        private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e){
            Log.Fatal(e.Exception.GetBaseException());
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e){
            Log.Fatal(e.ExceptionObject.ToString());
        }

Open in new window

Avatar of enrique_aeo

ASKER

is a web application and c # ap.net.
I am attaching the file to have a better idea of what I need. Regards
mostrarActividadDiscentes.txt
Hello Enrique:
You should use error handling whenever there is a chance that an error leave your app in a corrupt state. For example, in a teller machine transaction, if there is an error in a cash withdrawal, rollback transaction in order to return to previous account state. If you just want to be notified of errors in order to helping debugging, just put in Global.asax this code.
Suerte! ;)
void Application_Error(object sender, EventArgs e) {
        if (HttpContext.Current != null){
            if(HttpContext.Current.Error != null){
                LogError(HttpContext.Current.Error);
            }
            HttpContext.Current.Response.Redirect("~/ErrorPage.aspx");
        }
    }

static void LogError(Exception ex) {
        if (HttpContext.Current.User != null && HttpContext.Current.User.Identity.IsAuthenticated){
            log.Error("Exception. User:" + HttpContext.Current.User.Identity.Name);
        }
        log.Error(ex.GetBaseException().ToString());
    }

Open in new window

By the way, if you place all your buttons that you need to disable in the same aspnet panel, then you just have to disable this panel in order to get all content disabled ;)

private void DesabilitarBotones()
        {
            btnProcesarInformacion.Enabled = false;
            btnVisualizar.Disabled = true;
            btnImprimirArchivo.Visible = false;
            btnGenerarFolio.Disabled = true;
            btnGenerarInforme.Disabled = true;
            btnGenerarInformeConstanciaDetalladoMag.Disabled = true;
            btnGenerarInformeConstanciaDetalladoAux.Disabled = true;
        }
i nhave this error
Error 2 The name 'log' does not exist in the current context
errorLOG.png
Hello:
Yes, log is an instance of Log4Net I have declared somewhere else.
http://logging.apache.org/log4net/
But you can use anything you decide as you log provider. I just wanted to show you the rigth method for handling errors.
If you want to use log4net too, download it, add a reference to log4net.dll in you web app and configure it.


<configSections>
type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
<log4net>
		<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender">
			<layout type="log4net.Layout.PatternLayout">
				<conversionPattern value="%date - %message%newline"/>
			</layout>
		</appender>
		<appender name="GeneralLog" type="log4net.Appender.RollingFileAppender">
			<file value="Logs/general.txt"/>
			<appendToFile value="true"/>
			<maximumFileSize value="300KB"/>
			<rollingStyle value="Size"/>
			<maxSizeRollBackups value="5"/>
			<layout type="log4net.Layout.PatternLayout">
				<conversionPattern value="%d{dd-MM-yyyy HH:mm:ss.fff} [%t] %-5p %c - %m%n"/>
			</layout>
		</appender>
		<!-- levels: DEBUG, INFO, WARN, ERROR, FATAL -->
		<root>
			<level value="DEBUG"/>
			<appender-ref ref="GeneralLog"/>
		</root>
		<logger name="WebLog" additivity="false">
			<level value="ERROR"/>
			<appender-ref ref="GeneralLog"/>
		</logger>
</log4net>


And in Global.Asax:

private static readonly ILog log = LogManager.GetLogger("WebLog");

Open in new window

please
this portion of code is not clear to me

<configSections>
type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>
Hello Enrique:
That piece of xml goes in your web.config file. It informs aspnet infrastructure how to handle the log4net section that comes after.
More info about configSections here:
http://msdn.microsoft.com/en-us/library/ms228256%28v=VS.90%29.aspx
best regards.
hi, this is my change
<configSections>
    <sectionGroup name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
....
i have this error:
Unrecognized configuration section log4net/appender.

i attached my web.config

I am working with Visual Studio 2008
webCONFIG.txt
Hello Enrique:
Have you added a reference to log4net.dll in your web project?
Best regards.
<sectionGroup name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>

should be:

<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
Hi experts, The application is working
what I did to test the error handling is
1. stop the service of the database and got the following error
Error underlying provider Open.
I was hoping that I redirect to error page,
in any case as it should work?
forget the previous question, and is working correctly, ie is redirected to error page
My question is, where I see the error?
Enrique:
Youll fin the log in the path specified in web.config. If you copied it as is, it should be in the root of your web app, in the Logs folder.
Remember to give write acces to the aspnet user in that folder.
Best regards.
the folder exists, but I see no error
I see the error in event viewer
eventVIEWER.jpg
fodelSLN.jpg
ASKER CERTIFIED SOLUTION
Avatar of cubaman_24
cubaman_24
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
it is fine