Tuesday, 7 June 2016

Log Creation Or Managing Log File Date Wise In a Text File in Asp.net Using C#.Net

6/07/2016 - By Pranav Singh 0

This article will show you how you can create a or manage a log file date wise or write a log file date wise in a text file in asp.net using C#.net. In this I have shown process to create log text file and write the log into the text file in you asp.net application using c#.net.

So for this article first we will create a new asp.net application and add some button and a label control in the page. After adding the controls your page will look as shown below.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm12.aspx.cs" Inherits="WebApplication7.WebForm12" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Log Creation or Managing Log File Date Wise In a Text File in Asp.net Using  C#.Net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button 1 Clicked By User" OnClick="Button1_Click" />
        <br />
        <asp:Button ID="Button2" runat="server" Text="Button 2 Clicked By User" OnClick="Button2_Click" />
        <br />
        <asp:Button ID="Button4" runat="server" Text="Exception Button" OnClick="Button4_Click" />
        <br />
        <br />
        <asp:Label ID="lblmessage" runat="server" style="color: #FF0000" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

In above code I have added 2 buttons and a label control to create the log. Button1 and 2 will simply log the message and button3 will log the exception message.

Now check the below code.

public void LogCreation(string message)
        {
            if (!Directory.Exists(Server.MapPath("\\Log")))
            {
                Directory.CreateDirectory(Server.MapPath("\\Log"));
            }
            string logFilename = "Log_" + DateTime.Now.ToLongDateString() + ".txt";
            string logFilePath = @"\Log\" + logFilename;
            //Validate log file exist or not
            if (!File.Exists(Server.MapPath(logFilePath)))
            {
                File.Create(Server.MapPath(logFilePath));
            }
            message = DateTime.Now.ToString() + " : " + message;
            StreamWriter file = new StreamWriter(Server.MapPath(logFilePath), true);
            file.Write(message + "\r\n");

            file.Close();
        }

In above code I have first checked the log folder exists or not if it does not exists the code will create it. After validating the file we will validate the log text file as per date and if it does not then code will create it. After that assign date time for log message and write it into the text file. This is a ready to use code you can use it in your asp.net application very easily.

Now I will show you how you can use the above code in your application. Now check the below code.

  protected void Button1_Click(object sender, EventArgs e)
        {
            LogCreation("Click event by Button 1.");
            lblmessage.Text = "Log created successfully for Button 1";
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            LogCreation("Click event by Button 2.");
            lblmessage.Text = "Log created successfully for Button 2";
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            try
            {
                int z = 0;
                int val = 1 / z;
            }
            catch (Exception ex)
            {
                LogCreation("Exception - " + ex.Message.ToString());
            }
        }

In above code I have simple called the function and pass the message.  Now we have done run the application to check the output.


Now check the log file


About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top