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

Discussion in 'ASP .Net' started by Anonieko, Mar 24, 2006.

  1. Anonieko

    Anonieko Guest

    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!!");
    }
    }
    }
     
    Anonieko, Mar 24, 2006
    #1
    1. Advertising

  2. Anonieko

    Anonieko Guest

    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();
    }
    }
     
    Anonieko, Mar 25, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TmVpbCBCaWxsaW5naGFt?=

    Problems using __code reference in healthMonitoring/eventMappings

    =?Utf-8?B?TmVpbCBCaWxsaW5naGFt?=, May 4, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    1,993
    Brock Allen
    May 6, 2006
  2. =?Utf-8?B?cm9ibA==?=

    HealthMonitoring

    =?Utf-8?B?cm9ibA==?=, Jul 12, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    415
    =?Utf-8?B?cm9ibA==?=
    Jul 12, 2006
  3. =?Utf-8?B?SWFuIFBvd2VsbA==?=

    HealthMonitoring Provider

    =?Utf-8?B?SWFuIFBvd2VsbA==?=, Aug 4, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    414
    =?Utf-8?B?SWFuIFBvd2VsbA==?=
    Aug 4, 2006
  4. Dan
    Replies:
    2
    Views:
    442
  5. Howard Hoffman

    ASP.NET healthMonitoring WebErrorEvent / IIS7

    Howard Hoffman, Nov 13, 2008, in forum: ASP .Net
    Replies:
    2
    Views:
    1,696
    Howard Hoffman
    Dec 3, 2008
Loading...

Share This Page