Skip to main content

Posts

Showing posts from June, 2015

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(L…

Get error with details

You can log error with all details you want. Here is the example. You need to just pass the exception object & it will return you the whole error details which will be very helpful.  



public static string GetErrorInfo(Exception ex)
        {
            StringBuilder errorRows = new StringBuilder();

            StackTrace stackTrace = new StackTrace(ex, true);
            StackFrame stackFrame = stackTrace.GetFrame(stackTrace.FrameCount - 1);
            MethodBase methodBase = null;
            try
            {
                methodBase = stackFrame.GetMethod();
            }
            catch { }
            errorRows.AppendLine("<b>Server         : </b>" + System.Environment.MachineName + "<HR/>");
            errorRows.AppendLine("<b>Date Time         : </b>" + DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss") + "<HR/>");
            try
            {
                errorRows.AppendLine("<b>User    …