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
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
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
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
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());
}
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
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! ;)
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());
}
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.Ena bled = false;
btnVisualizar.Disabled = true;
btnImprimirArchivo.Visible = false;
btnGenerarFolio.Disabled = true;
btnGenerarInforme.Disabled = true;
btnGenerarInformeConstanci aDetallado Mag.Disabl ed = true;
btnGenerarInformeConstanci aDetallado Aux.Disabl ed = true;
}
private void DesabilitarBotones()
{
btnProcesarInformacion.Ena
btnVisualizar.Disabled = true;
btnImprimirArchivo.Visible
btnGenerarFolio.Disabled = true;
btnGenerarInforme.Disabled
btnGenerarInformeConstanci
btnGenerarInformeConstanci
}
ASKER
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.
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");
ASKER
please
this portion of code is not clear to me
<configSections>
type="log4net.Config.Log4N etConfigur ationSecti onHandler, log4net"/>
</configSections>
this portion of code is not clear to me
<configSections>
type="log4net.Config.Log4N
</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.
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.
ASKER
hi, this is my change
<configSections>
<sectionGroup name="log4net" type="log4net.Config.Log4N etConfigur ationSecti onHandler, log4net"/>
....
i have this error:
Unrecognized configuration section log4net/appender.
i attached my web.config
I am working with Visual Studio 2008
webCONFIG.txt
<configSections>
<sectionGroup name="log4net" type="log4net.Config.Log4N
....
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.
Have you added a reference to log4net.dll in your web project?
Best regards.
<sectionGroup name="log4net" type="log4net.Config.Log4N etConfigur ationSecti onHandler, log4net"/>
should be:
<section name="log4net" type="log4net.Config.Log4N etConfigur ationSecti onHandler, log4net"/>
should be:
<section name="log4net" type="log4net.Config.Log4N
ASKER
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?
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?
ASKER
forget the previous question, and is working correctly, ie is redirected to error page
My question is, where I see the error?
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.
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.
ASKER
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
it is fine
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.