locked
How can I write the exception to the SharePoint log (how can i create more then one categorie?) RRS feed

  • Question

  • Hey,

    I have wrote some code for SharePoint. On my development system its runs perfect.

    Now I like to the exception to the SharePoint log like

    Try
    {
         //some code
    }
    catch(Exception ex)
    {
         //write ex to the SharePoint log
    }

    How can I write the exception to the SharePoint log? I don’t want in the production system install visual studio but I like get some information about the errors. This will also be easier for administrators to find the problem.

    Thanks

    Stefan


    Viele Grüße Stefan

    Kontakt unter Info@IT-Kiessig.de


    • Edited by Stefan3110 Monday, January 21, 2013 9:05 PM
    Thursday, January 17, 2013 4:20 PM

Answers

  • Hi,

    Please refer the below post.

    http://blog.mastykarz.nl/logging-uls-sharepoint-2010/

    Regards,

    Srikanth

    • Proposed as answer by ChrisC_26 Thursday, January 17, 2013 4:43 PM
    • Marked as answer by Stefan3110 Thursday, January 17, 2013 5:00 PM
    Thursday, January 17, 2013 4:33 PM
  • Hi 

    Try this way,

    catch (Exception ex)
    {
         HandleException(ex);
    }

    and in use this function,

    private void HandleException(Exception ex)
            {
                this.Controls.Clear();
                this.Controls.Add(new LiteralControl(ex.Message));
            }

    In this way you can see the exception in your webpart control.

    The above is one best way.

    My next post will provide you another approach to write ULS logs.


    Thanks, Ali Yasir http://www.sharepointstack.blogspot.in/

    • Marked as answer by Stefan3110 Thursday, January 17, 2013 5:00 PM
    Thursday, January 17, 2013 4:46 PM
  • Hi,

    This will provide ULS logs


    Call this,

    catch (Exception ex)
                {
                    LogUtility.LogError(ex.StackTrace);
                    throw ex;
                }


    this catch method will call this below function,

    public static void LogError(string errorMsg)
            {
                LogInfo(errorMsg);
            }

    This will call :

     public static void LogInfo(string messageFormat, params object[] formatArgs)
            {
                SPDiagnosticsService diagSvc = SPDiagnosticsService.Local;
                diagSvc.WriteTrace(0, // custom trace id
                                    new SPDiagnosticsCategory("SharePointAMS.Commerce", TraceSeverity.High, EventSeverity.Error), // create a category
                                    TraceSeverity.High, // set the logging level of this record
                                    messageFormat, // custom message
                                    formatArgs // parameters to message
                                    );
            }
    

    This will help your case.


    Thanks, Ali Yasir http://www.sharepointstack.blogspot.in/

    • Marked as answer by Stefan3110 Thursday, January 17, 2013 5:00 PM
    Thursday, January 17, 2013 4:49 PM

All replies

  • Hi,

    Please refer the below post.

    http://blog.mastykarz.nl/logging-uls-sharepoint-2010/

    Regards,

    Srikanth

    • Proposed as answer by ChrisC_26 Thursday, January 17, 2013 4:43 PM
    • Marked as answer by Stefan3110 Thursday, January 17, 2013 5:00 PM
    Thursday, January 17, 2013 4:33 PM
  • Hi 

    Try this way,

    catch (Exception ex)
    {
         HandleException(ex);
    }

    and in use this function,

    private void HandleException(Exception ex)
            {
                this.Controls.Clear();
                this.Controls.Add(new LiteralControl(ex.Message));
            }

    In this way you can see the exception in your webpart control.

    The above is one best way.

    My next post will provide you another approach to write ULS logs.


    Thanks, Ali Yasir http://www.sharepointstack.blogspot.in/

    • Marked as answer by Stefan3110 Thursday, January 17, 2013 5:00 PM
    Thursday, January 17, 2013 4:46 PM
  • Hi,

    This will provide ULS logs


    Call this,

    catch (Exception ex)
                {
                    LogUtility.LogError(ex.StackTrace);
                    throw ex;
                }


    this catch method will call this below function,

    public static void LogError(string errorMsg)
            {
                LogInfo(errorMsg);
            }

    This will call :

     public static void LogInfo(string messageFormat, params object[] formatArgs)
            {
                SPDiagnosticsService diagSvc = SPDiagnosticsService.Local;
                diagSvc.WriteTrace(0, // custom trace id
                                    new SPDiagnosticsCategory("SharePointAMS.Commerce", TraceSeverity.High, EventSeverity.Error), // create a category
                                    TraceSeverity.High, // set the logging level of this record
                                    messageFormat, // custom message
                                    formatArgs // parameters to message
                                    );
            }
    

    This will help your case.


    Thanks, Ali Yasir http://www.sharepointstack.blogspot.in/

    • Marked as answer by Stefan3110 Thursday, January 17, 2013 5:00 PM
    Thursday, January 17, 2013 4:49 PM
  • How can I create more then one categories?

    Thnaks für help

    Stefan


    Viele Grüße Stefan

    Kontakt unter Info@IT-Kiessig.de

    Monday, January 21, 2013 9:03 PM