How to log to a text file in ASP.NET 2.0 using HealthMonitoring

A

Anonieko

Logging To a File In A S P . N E T 2 . 0 . I also want weekly rolling
filename for the log file.

Step 1. Create a class library ( 2 classes)
==============================

AppWebEvent.cs
--------------
using System;
using System.Web.Management;
using System.Text;
using System.Web;

// Implements a custom WebBaseEvent class.
public class AppWebEvent : WebBaseEvent
{
public AppWebEvent(string msg, object eventSource, int eventCode)
: base(msg, eventSource, eventCode) { }

public AppWebEvent(string msg, object eventSource, int eventCode,
int eventDetailCode)
: base(msg, eventSource, eventCode, eventDetailCode) { }
}


AppWebEventProvider.cs
-----------------------
using System;
using System.IO;
using System.Web.Management;
using System.Web;
using System.Collections.Specialized;

public class AppWebEventProvider : WebEventProvider
{
string _providerName = "";

public override void Initialize(string name, NameValueCollection
config)
{
_providerName = name;
}

public override void Flush() { }

public override void ProcessEvent(WebBaseEvent raisedEvent)
{
string path =
HttpContext.Current.Server.MapPath(@"logs\events" +
GetWeekEndingDay(DateTime.Now).ToString("yyMMdd") +".txt");

using (StreamWriter st = File.AppendText( path))
{
st.WriteLine("{0:yyyy/MM/dd HH:mm:ss:fff:zz}-{1}-{2}: {3}",
raisedEvent.EventTime,
HttpContext.Current.User.Identity.Name,
raisedEvent.EventSource,
raisedEvent.Message);
st.Flush();
}
}

public override void Shutdown() { }

private DateTime GetWeekEndingDay(DateTime dt)
{
while (dt.DayOfWeek != System.DayOfWeek.Sunday)
{
dt = dt.AddDays(1);
}
return dt;
}
}





Step 2. Add HealthMonitoring section in Web.Config file
=======================================================

<healthMonitoring enabled="true">
<providers>
<add name="MyOwnProvider" type="AppWebEventProvider" />

</providers>
<eventMappings>
<add name="MyEvent" type="AppWebEvent" />
</eventMappings>
<rules>
<add name="My Custom events" eventName="MyEvent"
provider="MyOwnProvider" minInterval="00:00:01"/>
</rules>

</healthMonitoring>





Step 3. Raise the event anywhere in your pages
==============================================

using System.Web.Management;
public partial class MyPage : System.Web.UI.Page
{
private void EventRaise(string msg)
{
new AppWebEvent(msg, this,
WebEventCodes.WebExtendedBase + 1).Raise();
}

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
EventRaise("Hello logging using 2.0 HealthMonitoring!!");
}
}
}
 
A

Anonieko

Add a check on the ProcessEvent() method above:


public override void ProcessEvent(WebBaseEvent raisedEvent)
{
if ( HttpContext.Current == null ) return; // Check first if
there is no http context.

string path =
HttpContext.Current.Server.MapPath(@"logs\events" +
GetWeekEndingDay(DateTime.Now).ToString("yyMMdd") +".txt");



using (StreamWriter st = File.AppendText( path))
{
st.WriteLine("{0:yyyy/MM/dd HH:mm:ss:fff:zz}-{1}-{2}: {3}",

raisedEvent.EventTime,
HttpContext.Current.User.Identity.Name,
raisedEvent.EventSource,
raisedEvent.Message);
st.Flush();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top