In SharePoint 2010 we have a very easy way to log errors in default SharePoint Log at "14\Log" folder by default. However if you are not able to view it here you can check Central Administrator --> Monitoring -->
Configure Diagonistic Logging -->Trace Log. Here is the code for :
Add a class file into your project and then paste below code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Runtime.InteropServices;
namespace LoggingServiceExample
{
public class LoggingService : SPDiagnosticsServiceBase
{
public static string vsDiagnosticAreaName = "SharePoint Logging Service";
public static string CategoryName = "SharePointProject";
public static uint uintEventID = 700; // Event ID
private static LoggingService _Current;
public static LoggingService Current
{
get
{
if (_Current == null)
{
_Current = new LoggingService();
}
return _Current;
}
}
private LoggingService()
: base("SharePoint Logging Service", SPFarm.Local)
{ }
protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()
{
List<SPDiagnosticsArea> areas = new List<SPDiagnosticsArea>
{
new SPDiagnosticsArea(vsDiagnosticAreaName, new List<SPDiagnosticsCategory>
{
new SPDiagnosticsCategory(CategoryName, TraceSeverity.Medium, EventSeverity.Error)
})
};
return areas;
}
public static string LogErrorInULS(string errorMessage)
{
string strExecutionResult = "Message Not Logged in ULS. ";
try
{
SPDiagnosticsCategory category = LoggingService.Current.Areas[vsDiagnosticAreaName].Categories[CategoryName];
LoggingService.Current.WriteTrace(uintEventID, category, TraceSeverity.Unexpected, errorMessage);
strExecutionResult = "Message Logged";
}
catch (Exception ex)
{
strExecutionResult += ex.Message;
}
return strExecutionResult;
}
public static string LogErrorInULS(string errorMessage, TraceSeverity tsSeverity)
{
string strExecutionResult = "Message Not Logged in ULS. ";
try
{
SPDiagnosticsCategory category = LoggingService.Current.Areas[vsDiagnosticAreaName].Categories[CategoryName];
LoggingService.Current.WriteTrace(uintEventID, category, tsSeverity, errorMessage);
strExecutionResult = "Message Logged";
}
catch (Exception ex)
{
strExecutionResult += ex.Message;
}
return strExecutionResult;
}
}
}
You just need to call following code on each entry points for any custom component :
try
{
}
catch(Exception ex)
{
LoggingService.LogErrorInULS("Custom SharePoint Application exception at " + System.DateTime.Now + "Exception Message : "+ ex.Message.ToString() +" Exception Stacktrace : "+ ex.StackTrace.ToString());
}
Configure Diagonistic Logging -->Trace Log. Here is the code for :
Add a class file into your project and then paste below code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Runtime.InteropServices;
namespace LoggingServiceExample
{
public class LoggingService : SPDiagnosticsServiceBase
{
public static string vsDiagnosticAreaName = "SharePoint Logging Service";
public static string CategoryName = "SharePointProject";
public static uint uintEventID = 700; // Event ID
private static LoggingService _Current;
public static LoggingService Current
{
get
{
if (_Current == null)
{
_Current = new LoggingService();
}
return _Current;
}
}
private LoggingService()
: base("SharePoint Logging Service", SPFarm.Local)
{ }
protected override IEnumerable<SPDiagnosticsArea> ProvideAreas()
{
List<SPDiagnosticsArea> areas = new List<SPDiagnosticsArea>
{
new SPDiagnosticsArea(vsDiagnosticAreaName, new List<SPDiagnosticsCategory>
{
new SPDiagnosticsCategory(CategoryName, TraceSeverity.Medium, EventSeverity.Error)
})
};
return areas;
}
public static string LogErrorInULS(string errorMessage)
{
string strExecutionResult = "Message Not Logged in ULS. ";
try
{
SPDiagnosticsCategory category = LoggingService.Current.Areas[vsDiagnosticAreaName].Categories[CategoryName];
LoggingService.Current.WriteTrace(uintEventID, category, TraceSeverity.Unexpected, errorMessage);
strExecutionResult = "Message Logged";
}
catch (Exception ex)
{
strExecutionResult += ex.Message;
}
return strExecutionResult;
}
public static string LogErrorInULS(string errorMessage, TraceSeverity tsSeverity)
{
string strExecutionResult = "Message Not Logged in ULS. ";
try
{
SPDiagnosticsCategory category = LoggingService.Current.Areas[vsDiagnosticAreaName].Categories[CategoryName];
LoggingService.Current.WriteTrace(uintEventID, category, tsSeverity, errorMessage);
strExecutionResult = "Message Logged";
}
catch (Exception ex)
{
strExecutionResult += ex.Message;
}
return strExecutionResult;
}
}
}
You just need to call following code on each entry points for any custom component :
try
{
}
catch(Exception ex)
{
LoggingService.LogErrorInULS("Custom SharePoint Application exception at " + System.DateTime.Now + "Exception Message : "+ ex.Message.ToString() +" Exception Stacktrace : "+ ex.StackTrace.ToString());
}
Hope it helps..!!
No comments:
Post a Comment