Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > How to log to a text file in ASP.NET 2.0 using HealthMonitoring

Reply
Thread Tools

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

 
 
Anonieko
Guest
Posts: n/a
 
      03-24-2006
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!!");
}
}
}

 
Reply With Quote
 
 
 
 
Anonieko
Guest
Posts: n/a
 
      03-25-2006
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();
}
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET 2.0 HealthMonitoring WebApplicationLifetimeEvent Dan ASP .Net 2 02-05-2007 09:28 PM
healthMonitoring document Q. John Chen ASP .Net 0 11-02-2006 04:47 PM
HealthMonitoring Provider =?Utf-8?B?SWFuIFBvd2VsbA==?= ASP .Net 0 08-04-2006 08:05 AM
HealthMonitoring =?Utf-8?B?cm9ibA==?= ASP .Net 0 07-12-2006 03:30 AM
Problems using __code reference in healthMonitoring/eventMappings =?Utf-8?B?TmVpbCBCaWxsaW5naGFt?= ASP .Net 1 05-06-2006 07:22 PM



Advertisments