how can i perform logging in sharepoint. i want to use tracing.
so that it logs in the 12 hive logs.
how can i perform logging in sharepoint. i want to use tracing.
so that it logs in the 12 hive logs.
(SharePoint 2007?) From Central Admin, go to Operations->Diagnostic Logging, "Trace Log" and "Event Throttling" are what you're looking for.
Select a category in "Event Throttling" and select least critical errors for both the event and trace logs. Then, select a path for the trace logs (mine was defaulted to ..12\LOGS) and supply a max number of logs and the number of minutes to use each log file.
I wrote some blog posts that will help you out. What i suggest is using the BCL logging classes (System.Diagnostics) and creating a custom TraceListner that writes to the SharePoint ULS logs.
http://sharepoint.nailhead.net/2008/04/instrumentation-logging-for-sharepoint.html
Microsoft provide an example:
http://msdn.microsoft.com/en-us/library/aa979522.aspx
This sample writes to the ULS log using the native trace methods, so a bit of pInvoke is used in the code.
You can then control the type of logging event in your code such as:
TraceProvider.WriteTrace(0, TraceProvider.TraceSeverity.High, Guid.Empty, "MyExeName", "Product Name", "Category Name", "Sample Message");
The event throttling settings in central admin will still be honored with this approach.
My preferred approach is to write a custom HttpModule to trap and log all errors. After logging the error, you can redirect the user to an error page - this is the approach I've seen most used for custom error handling in SharePoint.
In your HttpModule, you can use an approach such as the one described by Daniel to write the exceptions to the ULS logs.
Here's a simple example of doing this:
Create a class that implements IHttpModule, and wire up the appropriate event in the Http pipeline:
public void Init(HttpApplication context)
{
context.Error += new EventHandler(context_Error);
}
In the context_Error event, go through all the errors and log them ...
void context_Error(object sender, EventArgs e)
{
TraceProvider.RegisterTraceProvider();
foreach (var ex in HttpContext.Current.AllErrors)
{
TraceProvider.WriteTrace(0,
TraceProvider.TraceSeverity.Exception,
Guid.NewGuid(),
Assembly.GetExecutingAssembly().FullName,
"<your application name>",
"<exception category>",
ex.ToString());
}
TraceProvider.UnregisterTraceProvider();
HttpContext.Current.Server.ClearError();
HttpContext.Current.Response.Clear();
HttpContext.Current.Server.Transfer("/_layouts/Error500.aspx");
}
You of course have to wrap this all up into a feature (scoped at the Web Application level) and deploy it to SharePoint.
Note that your entry in the section of web.config for this custom error http module needs to be first in the list. The order in which the http modules are listed in the section matters, and the custom error http module should always execute first.
If you´re using MOSS, you can use this:
Microsoft.Office.Server.Diagnostics.PortalLog.LogString("Message");