Create Error Log Text File daily (every day) using C# and VB.Net in ASP.Net

mahesh213
 
on Nov 26, 2021 03:44 AM
Sample_355317.zip
596 Views

HI,

Create simple Error Log Text File in ASP.Net using C# and VB.Net

in above code logs are displaying properly in notepad

i have new requirement in same code

Want to log day by day on different notepad pages

ex: for today date(11/26/2021) display error page as ErrorLog_11_26_2021

if your going to log errors on next day display error page as ErrorLog_11_27_2021

Note:Create pages automatically based upon Date

ex: if your logging errors today create notepad page automatically with today date like that i am going keep logs on different pages

can you please help me

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Nov 26, 2021 05:59 AM

Hi mahesh213,

Check with below example.

HTML

<asp:Button Text="Click to Raise Exception" runat="server" OnClick="RaiseException" />

Code

C#

protected void RaiseException(object sender, EventArgs e)
{
    try
    {
        int i = int.Parse("Mudassar");
    }
    catch (Exception ex)
    {
        this.LogError(ex);
    }
}

private void LogError(Exception ex)
{
    string fileName = string.Format("ErrorLog_{0}.txt", DateTime.Now.ToString("MM_dd_yyyy"));
    string path = Server.MapPath("~/ErrorLog/") + fileName;

    string message = string.Format("Time: {0}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"));
    message += Environment.NewLine;
    message += "-----------------------------------------------------------";
    message += Environment.NewLine;
    message += string.Format("Message: {0}", ex.Message);
    message += Environment.NewLine;
    message += string.Format("StackTrace: {0}", ex.StackTrace);
    message += Environment.NewLine;
    message += string.Format("Source: {0}", ex.Source);
    message += Environment.NewLine;
    message += string.Format("TargetSite: {0}", ex.TargetSite.ToString());
    message += Environment.NewLine;
    message += "-----------------------------------------------------------";
    message += Environment.NewLine;
    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(path, true))
    {
        writer.WriteLine(message);
        writer.Close();
    }
}

VB.Net

Protected Sub RaiseException(ByVal sender As Object, ByVal e As EventArgs)
    Try
        Dim i As Integer = Integer.Parse("Mudassar")
    Catch ex As Exception
        Me.LogError(ex)
    End Try
End Sub

Private Sub LogError(ByVal ex As Exception)
    Dim fileName As String = String.Format("ErrorLog_{0}.txt", DateTime.Now.ToString("MM_dd_yyyy"))
    Dim path As String = Server.MapPath("~/ErrorLog/") & fileName
    Dim message As String = String.Format("Time: {0}", DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt"))
    message += Environment.NewLine
    message += "-----------------------------------------------------------"
    message += Environment.NewLine
    message += String.Format("Message: {0}", ex.Message)
    message += Environment.NewLine
    message += String.Format("StackTrace: {0}", ex.StackTrace)
    message += Environment.NewLine
    message += String.Format("Source: {0}", ex.Source)
    message += Environment.NewLine
    message += String.Format("TargetSite: {0}", ex.TargetSite.ToString())
    message += Environment.NewLine
    message += "-----------------------------------------------------------"
    message += Environment.NewLine
    Using writer As IO.StreamWriter = New IO.StreamWriter(path, True)
        writer.WriteLine(message)
        writer.Close()
    End Using
End Sub