[newbie] EventLog log = new EventLog() does not work within catch {} block
-
I'm trying to run this code within Visual Studio 2010, however, having set a breakpoint at line 39, the execution does not seem to get to line 39 where I have set a break point. As a matter of fact, when checking under Visual Studio 2010 Server Explorer > Servers > {puter name} > Application > ASP.NET 4.0.30319.0, there are no relevant events written. I also tried checking within the Windows Event Viewer. For Win XP (Control Panel > Administrative Tools > Event Viewer > Application sub node). Is this a bug with .NET 4 or Visual Studio 2010? Does this issue happen to anyone else? :confused:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Diagnostics;public partial class ErrorTestLog : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{} protected void cmdCompute\_Click(object sender, EventArgs e) { try { decimal a, b, result; a = Decimal.Parse(txtA.Text); b = Decimal.Parse(txtB.Text); result = a / b; lblResult.Text = result.ToString(); lblResult.ForeColor = Color.Black; } catch (Exception err) { lblResult.Text = "<b>Message:</b> " + err.Message + "<br /><br />"; lblResult.Text += "<b>Source:</b> " + err.Source + "<br /><br />"; lblResult.Text += "<b>Stack Trace:</b> " + err.StackTrace; lblResult.ForeColor = Color.Red; // Write the information to the event log. EventLog log = new EventLog(); log.Source = "DivisionPage"; log.WriteEntry(err.Message, EventLogEntryType.Error); } }
}
NOTE: 1. Beginning ASP.NET 3.5 in C# in 2008 - From Novice to Professional (ISBN: 978-1-59059-891-7) P.268 (306)
Jon
-
I'm trying to run this code within Visual Studio 2010, however, having set a breakpoint at line 39, the execution does not seem to get to line 39 where I have set a break point. As a matter of fact, when checking under Visual Studio 2010 Server Explorer > Servers > {puter name} > Application > ASP.NET 4.0.30319.0, there are no relevant events written. I also tried checking within the Windows Event Viewer. For Win XP (Control Panel > Administrative Tools > Event Viewer > Application sub node). Is this a bug with .NET 4 or Visual Studio 2010? Does this issue happen to anyone else? :confused:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Diagnostics;public partial class ErrorTestLog : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{} protected void cmdCompute\_Click(object sender, EventArgs e) { try { decimal a, b, result; a = Decimal.Parse(txtA.Text); b = Decimal.Parse(txtB.Text); result = a / b; lblResult.Text = result.ToString(); lblResult.ForeColor = Color.Black; } catch (Exception err) { lblResult.Text = "<b>Message:</b> " + err.Message + "<br /><br />"; lblResult.Text += "<b>Source:</b> " + err.Source + "<br /><br />"; lblResult.Text += "<b>Stack Trace:</b> " + err.StackTrace; lblResult.ForeColor = Color.Red; // Write the information to the event log. EventLog log = new EventLog(); log.Source = "DivisionPage"; log.WriteEntry(err.Message, EventLogEntryType.Error); } }
}
NOTE: 1. Beginning ASP.NET 3.5 in C# in 2008 - From Novice to Professional (ISBN: 978-1-59059-891-7) P.268 (306)
Jon
Does your exception block throw an exception? Do you have a generic exception handler? Does your IIS user have access to write to the event log? Did you use System.Diagnostics.EventLog.CreateEventSource and System.Diagnostics.EventLog.WriteEntry? (Probably not, since you new'd EventLog)
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane
-
I'm trying to run this code within Visual Studio 2010, however, having set a breakpoint at line 39, the execution does not seem to get to line 39 where I have set a break point. As a matter of fact, when checking under Visual Studio 2010 Server Explorer > Servers > {puter name} > Application > ASP.NET 4.0.30319.0, there are no relevant events written. I also tried checking within the Windows Event Viewer. For Win XP (Control Panel > Administrative Tools > Event Viewer > Application sub node). Is this a bug with .NET 4 or Visual Studio 2010? Does this issue happen to anyone else? :confused:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Diagnostics;public partial class ErrorTestLog : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{} protected void cmdCompute\_Click(object sender, EventArgs e) { try { decimal a, b, result; a = Decimal.Parse(txtA.Text); b = Decimal.Parse(txtB.Text); result = a / b; lblResult.Text = result.ToString(); lblResult.ForeColor = Color.Black; } catch (Exception err) { lblResult.Text = "<b>Message:</b> " + err.Message + "<br /><br />"; lblResult.Text += "<b>Source:</b> " + err.Source + "<br /><br />"; lblResult.Text += "<b>Stack Trace:</b> " + err.StackTrace; lblResult.ForeColor = Color.Red; // Write the information to the event log. EventLog log = new EventLog(); log.Source = "DivisionPage"; log.WriteEntry(err.Message, EventLogEntryType.Error); } }
}
NOTE: 1. Beginning ASP.NET 3.5 in C# in 2008 - From Novice to Professional (ISBN: 978-1-59059-891-7) P.268 (306)
Jon
-
Does your exception block throw an exception? Do you have a generic exception handler? Does your IIS user have access to write to the event log? Did you use System.Diagnostics.EventLog.CreateEventSource and System.Diagnostics.EventLog.WriteEntry? (Probably not, since you new'd EventLog)
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane
Does your exception block throw an exception? No, but throwing the exception prior to calling the event log makes the logging code unreachable, and, this works fine within Visual Studio 2010. Do you have a generic exception handler? No, because it is not required for now. Does your IIS user have access to write to the event log? I am using Administrator on a local PC, what user does VS 2010 use (to confirm)? Did you use System.Diagnostics.EventLog.CreateEventSource and System.Diagnostics.EventLog.WriteEntry? (Probably not, since you new'd EventLog) System.Diagnostics.CreateEventSource is not even shown up within Visual Studio 2010, even though it's a static method. Problem with Intellisense maybe.
Jon