how to generate log file in website using c#

how to generate log file in website using c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Net;
using context = System.Web.HttpContext;

namespace ajayvishu
{
    public static class ExceptionLogging
    {
        private static String ErrorlineNo, Errormsg, extype, exurl, hostIp, ErrorLocation, HostAdd,ServerName;
        public static void SendErrorToText(Exception ex)
        {
            //Server Name Get From User Side
            ServerName=Server.MachineName;
            //New Line Display Text
            var line = Environment.NewLine + Environment.NewLine;
            //Display Number Of Line Error Generate
            ErrorlineNo = ex.StackTrace.Substring(ex.StackTrace.Length - 7, 7);
            //Actual Error Message To Display
            Errormsg = ex.GetType().Name.ToString();
            //Error Message Type Here
            extype = ex.GetType().ToString();
            //Browser Url For Access Page thats
            exurl = context.Current.Request.Url.ToString();
            //Use In Inbuilt Class IPHostEntry Create object store value like as ipaddress and hostaddress(*physical address)   
            IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
            //Ipaddress value in array index 1 position
            hostIp = ipHostInfo.AddressList[1].ToString();
            //Network UserName
            HostAdd = ipHostInfo.HostName+"  "+ipHostInfo.AddressList[0];
            //Error Message
            ErrorLocation = ex.Message.ToString();
            try
            {
                //Text File Path
                string filepath = context.Current.Server.MapPath("~/ExceptionDetailsFile/");
                if (!Directory.Exists(filepath))
                {
                    //Create Directory Folder Inside txt File
                    Directory.CreateDirectory(filepath);
                }
                //Text File Name
                filepath = filepath + DateTime.Today.ToString("dd-MM-yy") + ".txt"; 
                if (!File.Exists(filepath))
                {
                    File.Create(filepath).Dispose();
                }
                using (StreamWriter sw = File.AppendText(filepath))
                {
                    //All Error Message Here Seperated By New Line
                    string error = "Server Name From User:" + " " + ServerName + line +"Log Written Date:" + " " + DateTime.Now.ToString() + line + "Error Line No :" + " " + ErrorlineNo + line + "Error Message:" + " " + Errormsg + line + "Exception Type:" + " " + extype + line + "Error Location :" + " " + ErrorLocation + line + " Error Page Url:" + " " + exurl + line + "User Host IP:" + " " + hostIp + line+ "User Host Address:" + " " + HostAdd + line;
                    sw.WriteLine("-----------Exception Details on " + " " + DateTime.Now.ToString() + "-----------------");
                    sw.WriteLine("----------------------------------------------------------------------------------");
                    sw.WriteLine(line);
                    sw.WriteLine(error);
                    sw.WriteLine("--------------------------------*End*------------------------------------------");
                    sw.WriteLine(line);
                    sw.Flush();
                    sw.Close();
                }
            }
            catch (Exception e)
            {
                e.ToString();
            }
        }
    }
}
how to generate log file in website using c#
download project


Post a Comment

0 Comments