Log error in a separate file
You can log your applications errors in separate text file. Use this code
public static void WriteException(string msg)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
string LogFile = SPUtility.GetVersionedGenericSetupPath("LOGS",15) + "\\SPErrorLog-" + DateTime.Today.ToString("MM-dd-yyyy") + ".txt";
StringBuilder sMessage = new StringBuilder();
msg = msg.Replace("<b>", "");
msg = msg.Replace("</b>", "");
msg = msg.Replace("<HR/>", "");
sMessage.AppendLine("--------------------------------------------------------------------------------------------------------------------------------------------");
sMessage.AppendLine(msg);
if (!File.Exists(LogFile))
{
StreamWriter sw = File.CreateText(LogFile);
sw.WriteLine(sMessage);
sw.Close();
}
else
{
StreamWriter sw = File.AppendText(LogFile);
sw.WriteLine(sMessage);
sw.Close();
}
});
}
catch { }
}
public static void WriteException(string msg)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
string LogFile = SPUtility.GetVersionedGenericSetupPath("LOGS",15) + "\\SPErrorLog-" + DateTime.Today.ToString("MM-dd-yyyy") + ".txt";
StringBuilder sMessage = new StringBuilder();
msg = msg.Replace("<b>", "");
msg = msg.Replace("</b>", "");
msg = msg.Replace("<HR/>", "");
sMessage.AppendLine("--------------------------------------------------------------------------------------------------------------------------------------------");
sMessage.AppendLine(msg);
if (!File.Exists(LogFile))
{
StreamWriter sw = File.CreateText(LogFile);
sw.WriteLine(sMessage);
sw.Close();
}
else
{
StreamWriter sw = File.AppendText(LogFile);
sw.WriteLine(sMessage);
sw.Close();
}
});
}
catch { }
}
Comments