ErrorLog.CS
using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Application1
{
public class ErrorLog
{
public class ErrorLog
{
/// <summary>
/// WriteToEventLog
/// </summary>
/// <param name="message"></param>
/// <param name="sourcename"></param>
/// <param name="logName"></param>
#region WriteToEventLog
public void WriteToEventLog(string message, string sourcename, string logName, EventLogEntryType ErrorType)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
EventLog elog = new EventLog();
if (!EventLog.SourceExists(sourcename))
{
EventLog.CreateEventSource(sourcename, logName);
}
elog.Source = sourcename;
elog.Log = logName;
elog.EnableRaisingEvents = true;
elog.WriteEntry(message, ErrorType);
});
}
catch (Exception ex)
{
writeErrors(ex.ToString());
}
}
#endregion
/// <summary>
/// writeErrors
/// </summary>
/// <param name="msg"></param>
#region writeErrors
public void writeErrors(string msg)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
string dir = @"c:\\ Error LOG";
Directory.CreateDirectory(dir);
TextWriter tw = new StreamWriter(@"c:\\ Error LOG\Error.txt", true);
tw.WriteLine(System.DateTime.Now.ToString() + " Error : " + msg);
tw.Close();
});
}
catch (Exception ex)
{
//writeErrors(ex.ToString());
}
}
#endregion
}
}
}
#region ErrorLog Variables
public string sourceName = "Application1";
public string logName = "Application";
ErrorLog el = new ErrorLog();
#endregion
Writing ErrorLog method
/// <summary> ErrorLogMethod
/// ErrorLogMethod
/// </summary>
/// <param name="ex"></param>
private void ErrorLogMethod(Exception ex)
{
//Catching error in Event viewer
el.writeErrors(ex.ToString());
el.WriteToEventLog(ex.ToString(), sourceName, logName, EventLogEntryType.Error);
//Displaying the Error Message
lblerror.Text = ex.Message;
}
protected void Page_Load(object sender, EventArgs e)
{
try
{
}
catch (Exception ex)
{
ErrorLogMethod(ex);
}
}
using Microsoft.SharePoint;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Application1
{
public class ErrorLog
{
public class ErrorLog
{
/// <summary>
/// WriteToEventLog
/// </summary>
/// <param name="message"></param>
/// <param name="sourcename"></param>
/// <param name="logName"></param>
#region WriteToEventLog
public void WriteToEventLog(string message, string sourcename, string logName, EventLogEntryType ErrorType)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
EventLog elog = new EventLog();
if (!EventLog.SourceExists(sourcename))
{
EventLog.CreateEventSource(sourcename, logName);
}
elog.Source = sourcename;
elog.Log = logName;
elog.EnableRaisingEvents = true;
elog.WriteEntry(message, ErrorType);
});
}
catch (Exception ex)
{
writeErrors(ex.ToString());
}
}
#endregion
/// <summary>
/// writeErrors
/// </summary>
/// <param name="msg"></param>
#region writeErrors
public void writeErrors(string msg)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
string dir = @"c:\\ Error LOG";
Directory.CreateDirectory(dir);
TextWriter tw = new StreamWriter(@"c:\\ Error LOG\Error.txt", true);
tw.WriteLine(System.DateTime.Now.ToString() + " Error : " + msg);
tw.Close();
});
}
catch (Exception ex)
{
//writeErrors(ex.ToString());
}
}
#endregion
}
}
}
Application1.cs
Creating Error Log variables#region ErrorLog Variables
public string sourceName = "Application1";
public string logName = "Application";
ErrorLog el = new ErrorLog();
#endregion
Writing ErrorLog method
/// <summary> ErrorLogMethod
/// ErrorLogMethod
/// </summary>
/// <param name="ex"></param>
private void ErrorLogMethod(Exception ex)
{
//Catching error in Event viewer
el.writeErrors(ex.ToString());
el.WriteToEventLog(ex.ToString(), sourceName, logName, EventLogEntryType.Error);
//Displaying the Error Message
lblerror.Text = ex.Message;
}
protected void Page_Load(object sender, EventArgs e)
{
try
{
}
catch (Exception ex)
{
ErrorLogMethod(ex);
}
}