By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,285 Members | 2,109 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,285 IT Pros & Developers. It's quick & easy.

How To: write to EventLog from .NET 2.0 web app?

P: n/a
I'm having difficulty writing anything to the web servers event log from my
web app located on that server.

Putting this in my web apps web.config did NOT help - immediate causes and
error in my web app before it displays the default page.

<healthMonitoring>
<rules>
<add name="All Events" eventName="All Events" provider="EventLogProvider"
profile="Critical" />
</rules>
</healthMonitoring>
How can I write my own events to the EventLog?
Jun 7 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Rob,
This is all it takes:

protected void Page_Load(object sender, EventArgs e)
{
System.Diagnostics.EventLog.WriteEntry("MyApp", "Howdy!");

}

Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Rob R. Ainscough" wrote:
I'm having difficulty writing anything to the web servers event log from my
web app located on that server.

Putting this in my web apps web.config did NOT help - immediate causes and
error in my web app before it displays the default page.

<healthMonitoring>
<rules>
<add name="All Events" eventName="All Events" provider="EventLogProvider"
profile="Critical" />
</rules>
</healthMonitoring>
How can I write my own events to the EventLog?

Jun 7 '06 #2

P: n/a
I tried that and nothing happens on my web server -- Event Viewer shows no
entries at all for MyApp.

If I run the exact same code locally via VS 2005 it works (as in I see the
entries in the Event Viewer Applications) -- so I'm guess I need something
more than just code? Security settings and/or web.config or something?

Any suggestions?

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:4C**********************************@microsof t.com...
Rob,
This is all it takes:

protected void Page_Load(object sender, EventArgs e)
{
System.Diagnostics.EventLog.WriteEntry("MyApp", "Howdy!");

}

Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Rob R. Ainscough" wrote:
I'm having difficulty writing anything to the web servers event log from
my
web app located on that server.

Putting this in my web apps web.config did NOT help - immediate causes
and
error in my web app before it displays the default page.

<healthMonitoring>
<rules>
<add name="All Events" eventName="All Events"
provider="EventLogProvider"
profile="Critical" />
</rules>
</healthMonitoring>
How can I write my own events to the EventLog?

Jun 7 '06 #3

P: n/a
The account the web page is running under needs permission to write to the
event log. You can either grant additional permissions to the default aspnet
account (if that's how your app is running) or use the <impersonation ="true
....
web.config element to have your app run under a more privileged account.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Rob R. Ainscough" wrote:
I tried that and nothing happens on my web server -- Event Viewer shows no
entries at all for MyApp.

If I run the exact same code locally via VS 2005 it works (as in I see the
entries in the Event Viewer Applications) -- so I'm guess I need something
more than just code? Security settings and/or web.config or something?

Any suggestions?

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:4C**********************************@microsof t.com...
Rob,
This is all it takes:

protected void Page_Load(object sender, EventArgs e)
{
System.Diagnostics.EventLog.WriteEntry("MyApp", "Howdy!");

}

Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Rob R. Ainscough" wrote:
I'm having difficulty writing anything to the web servers event log from
my
web app located on that server.

Putting this in my web apps web.config did NOT help - immediate causes
and
error in my web app before it displays the default page.

<healthMonitoring>
<rules>
<add name="All Events" eventName="All Events"
provider="EventLogProvider"
profile="Critical" />
</rules>
</healthMonitoring>
How can I write my own events to the EventLog?


Jun 7 '06 #4

P: n/a
Yes this a security issue for sure:

System.Security.SecurityException: Requested registry access is not allowed.
at System.ThrowHelper.ThrowSecurityException(Exceptio nResource resource) at
Microsoft.Win32.RegistryKey.OpenSubKey(String name, Boolean writable) at
System.Diagnostics.EventLog.CreateEventSource(Even tSourceCreationData
sourceData) at System.Diagnostics.EventLog.VerifyAndCreateSource( String
sourceName, String currentMachineName) at
System.Diagnostics.EventLog.WriteEntry(String message, EventLogEntryType
type, Int32 eventID, Int16 category, Byte[] rawData) at
System.Diagnostics.EventLog.WriteEntry(String source, String message,
EventLogEntryType type, Int32 eventID, Int16 category, Byte[] rawData) at
System.Diagnostics.EventLog.WriteEntry(String source, String message) at
..... The Zone of the assembly that failed was: MyComputer

should I impersonate administrator? I thought using impersonation was a
security problem?

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:58**********************************@microsof t.com...
The account the web page is running under needs permission to write to the
event log. You can either grant additional permissions to the default
aspnet
account (if that's how your app is running) or use the <impersonation
="true
...
web.config element to have your app run under a more privileged account.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Rob R. Ainscough" wrote:
I tried that and nothing happens on my web server -- Event Viewer shows
no
entries at all for MyApp.

If I run the exact same code locally via VS 2005 it works (as in I see
the
entries in the Event Viewer Applications) -- so I'm guess I need
something
more than just code? Security settings and/or web.config or something?

Any suggestions?

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in
message
news:4C**********************************@microsof t.com...
> Rob,
> This is all it takes:
>
> protected void Page_Load(object sender, EventArgs e)
> {
> System.Diagnostics.EventLog.WriteEntry("MyApp", "Howdy!");
>
> }
>
> Peter
> --
> Co-founder, Eggheadcafe.com developer portal:
> http://www.eggheadcafe.com
> UnBlog:
> http://petesbloggerama.blogspot.com
>
>
>
>
> "Rob R. Ainscough" wrote:
>
>> I'm having difficulty writing anything to the web servers event log
>> from
>> my
>> web app located on that server.
>>
>> Putting this in my web apps web.config did NOT help - immediate causes
>> and
>> error in my web app before it displays the default page.
>>
>> <healthMonitoring>
>> <rules>
>> <add name="All Events" eventName="All Events"
>> provider="EventLogProvider"
>> profile="Critical" />
>> </rules>
>> </healthMonitoring>
>>
>>
>> How can I write my own events to the EventLog?
>>
>>
>>


Jun 7 '06 #5

P: n/a
No. Never. It would be a security issue as you mentioned.

The problem in creating the EventLog entry is because the ASP Account
can not create the EVENT SOURCE. As long as you created the event
source in the system. WriteEntry() will be ok if it writes to an
existing event source.

To Create an event souce:

1. In registry under
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Servic es\Eventlog\Application\
add a subkey (MyApp in your case).

2. Or, Try to create the above mentioned registry key automatically
such as in the installer or a setup utility.
John

Jun 7 '06 #6

P: n/a
John,

Thanks for the response. I created the MyApp entry and added read/write
permissions for ASP.NET and NETWORK SERVICES accounts on the reg key
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Servic es\Eventlog\Application\
-- that solved the problem.

I don't like to do impersonate in web.config, just too risky IMHO.

Rob.

"Q. John Chen" <qj****@email.com> wrote in message
news:11*********************@i40g2000cwc.googlegro ups.com...
No. Never. It would be a security issue as you mentioned.

The problem in creating the EventLog entry is because the ASP Account
can not create the EVENT SOURCE. As long as you created the event
source in the system. WriteEntry() will be ok if it writes to an
existing event source.

To Create an event souce:

1. In registry under
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Servic es\Eventlog\Application\
add a subkey (MyApp in your case).

2. Or, Try to create the above mentioned registry key automatically
such as in the installer or a setup utility.
John

Jun 8 '06 #7

P: n/a
Rob,

Glad it helped. I just thought that you don't have to add permissions
for ASP.NET and NETWORK SERVICE.

John

Jun 8 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.