P
Patrick
I have the following code, which regardless which works fine and logs to the
EventViewer regardless of whether
<processModel/> section of machine.config is set to username="SYSTEM" or
"machine"
---Start of test.aspx----
<%@ Page language="C#" AutoEventWireup="false" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title> test</title>
</HEAD>
<body>
<%
try
{
System.Diagnostics.EventLog objEventLog;
if (!System.Diagnostics.EventLog.SourceExists("TESTSOURCE"))
{
System.Diagnostics.EventLog.CreateEventSource("TESTSOURCE","Application");
}
objEventLog = new System.Diagnostics.EventLog();
objEventLog.Source = "TESTSOURCE";
if ( objEventLog.Log.ToUpper() != "APPLICATION" )
{
System.Console.WriteLine("Some other application is
using the source!");
return;
}
objEventLog.WriteEntry("Message",
System.Diagnostics.EventLogEntryType.Error);
}
catch (Exception e)
{
%>
<%=e%>
<%
} //end try
%>
</body>
</HTML>
----End of test.aspx----
However, if I have some very similar code in a Logging.cs file as follows:
--Start of logging.cs--
using System;
using System.Diagnostics;
namespace website.classes
{
public class Logging
{
private const string EVENT_SOURCE = "YJBWEBSITE";
private const string EVENT_LOG = "Applicaiton";
public Logging()
{
//Default constructor
}
public static void LogEventViewer(string
strMessage,EventLogEntryType objLogEntryType)
{
try
{
EventLog objEventLog;
if (!EventLog.SourceExists(EVENT_SOURCE))
{
EventLog.CreateEventSource(EVENT_SOURCE,EVENT_LOG);
}
objEventLog = new EventLog();
objEventLog.Source = EVENT_SOURCE;
if ( objEventLog.Log.ToUpper() != EVENT_LOG.ToUpper() )
{
System.Console.WriteLine("Some other application is
using the source!");
return;
}
objEventLog.WriteEntry(strMessage,objLogEntryType);
}
catch (Exception e)
{
System.Console.WriteLine(e);
return;
} //end try
} //end LogEventViewer
public static void LogError(string strMessage)
{
LogEventViewer(strMessage,EventLogEntryType.Error);
}
} //end class Logging
}
---end of logging.cs--
then, whenver I try to call from ASPX Logging.LogError("an error message"),
I got an error, and when I debug the code, I found that it is failing on the
call to EventLog.SourceExists.
{"Requested registry access is not allowed." }
[System.Security.SecurityException]: {"Requested registry access is not
allowed."}
System.Object: {System.Security.SecurityException}
_className: null
_COMPlusExceptionCode: -532459699
_exceptionMethod: <undefined value>
_exceptionMethodString: null
_helpURL: null
_HResult: -2146233078
_innerException: { }
_message: "Requested registry access is not allowed."
_remoteStackIndex: 0
_remoteStackTraceString: null
_source: null
_stackTrace: {System.Array}
_stackTraceString: null
_xcode: -532459699
_xptrs: 0
HelpLink: null
HResult: -2146233078
InnerException: { }
Message: "Requested registry access is not allowed."
Source: "mscorlib"
StackTrace: " at Microsoft.Win32.RegistryKey.OpenSubKey(String name,
Boolean writable)\r\n at
System.Diagnostics.EventLog.FindSourceRegistration(String source, String
machineName, Boolean readOnly)\r\n at
System.Diagnostics.EventLog.SourceExists(String source, String
machineName)\r\n at System.Diagnostics.EventLog.SourceExists(String
source)\r\n at YJB_Website.classes.Logging.LogEventViewer(String
strMessage, EventLogEntryType objLogEntryType) in
c:\\inetpub\\wwwroot\\yjb_website\\classes\\logging.cs:line 74"
TargetSite: {System.Reflection.RuntimeMethodInfo}
The only way I could get this working is to set <processModel />,
userName="SYSTEM".
But this is not desirable as I don't want to high level of access. and I am
surprised why the code works when run from ASPX but not from a class? Note
also I also have the following settings on machine.config
<identity impersonate="false" userName="Domain\user"
password="password"/>
EventViewer regardless of whether
<processModel/> section of machine.config is set to username="SYSTEM" or
"machine"
---Start of test.aspx----
<%@ Page language="C#" AutoEventWireup="false" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title> test</title>
</HEAD>
<body>
<%
try
{
System.Diagnostics.EventLog objEventLog;
if (!System.Diagnostics.EventLog.SourceExists("TESTSOURCE"))
{
System.Diagnostics.EventLog.CreateEventSource("TESTSOURCE","Application");
}
objEventLog = new System.Diagnostics.EventLog();
objEventLog.Source = "TESTSOURCE";
if ( objEventLog.Log.ToUpper() != "APPLICATION" )
{
System.Console.WriteLine("Some other application is
using the source!");
return;
}
objEventLog.WriteEntry("Message",
System.Diagnostics.EventLogEntryType.Error);
}
catch (Exception e)
{
%>
<%=e%>
<%
} //end try
%>
</body>
</HTML>
----End of test.aspx----
However, if I have some very similar code in a Logging.cs file as follows:
--Start of logging.cs--
using System;
using System.Diagnostics;
namespace website.classes
{
public class Logging
{
private const string EVENT_SOURCE = "YJBWEBSITE";
private const string EVENT_LOG = "Applicaiton";
public Logging()
{
//Default constructor
}
public static void LogEventViewer(string
strMessage,EventLogEntryType objLogEntryType)
{
try
{
EventLog objEventLog;
if (!EventLog.SourceExists(EVENT_SOURCE))
{
EventLog.CreateEventSource(EVENT_SOURCE,EVENT_LOG);
}
objEventLog = new EventLog();
objEventLog.Source = EVENT_SOURCE;
if ( objEventLog.Log.ToUpper() != EVENT_LOG.ToUpper() )
{
System.Console.WriteLine("Some other application is
using the source!");
return;
}
objEventLog.WriteEntry(strMessage,objLogEntryType);
}
catch (Exception e)
{
System.Console.WriteLine(e);
return;
} //end try
} //end LogEventViewer
public static void LogError(string strMessage)
{
LogEventViewer(strMessage,EventLogEntryType.Error);
}
} //end class Logging
}
---end of logging.cs--
then, whenver I try to call from ASPX Logging.LogError("an error message"),
I got an error, and when I debug the code, I found that it is failing on the
call to EventLog.SourceExists.
{"Requested registry access is not allowed." }
[System.Security.SecurityException]: {"Requested registry access is not
allowed."}
System.Object: {System.Security.SecurityException}
_className: null
_COMPlusExceptionCode: -532459699
_exceptionMethod: <undefined value>
_exceptionMethodString: null
_helpURL: null
_HResult: -2146233078
_innerException: { }
_message: "Requested registry access is not allowed."
_remoteStackIndex: 0
_remoteStackTraceString: null
_source: null
_stackTrace: {System.Array}
_stackTraceString: null
_xcode: -532459699
_xptrs: 0
HelpLink: null
HResult: -2146233078
InnerException: { }
Message: "Requested registry access is not allowed."
Source: "mscorlib"
StackTrace: " at Microsoft.Win32.RegistryKey.OpenSubKey(String name,
Boolean writable)\r\n at
System.Diagnostics.EventLog.FindSourceRegistration(String source, String
machineName, Boolean readOnly)\r\n at
System.Diagnostics.EventLog.SourceExists(String source, String
machineName)\r\n at System.Diagnostics.EventLog.SourceExists(String
source)\r\n at YJB_Website.classes.Logging.LogEventViewer(String
strMessage, EventLogEntryType objLogEntryType) in
c:\\inetpub\\wwwroot\\yjb_website\\classes\\logging.cs:line 74"
TargetSite: {System.Reflection.RuntimeMethodInfo}
The only way I could get this working is to set <processModel />,
userName="SYSTEM".
But this is not desirable as I don't want to high level of access. and I am
surprised why the code works when run from ASPX but not from a class? Note
also I also have the following settings on machine.config
<identity impersonate="false" userName="Domain\user"
password="password"/>