Hi guys,
Last monday, we have released drop 7 of the SharePoint Guidance V2. You can get it here:
http://spg.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=26085
In this drop, we have tackled a couple of things:
For your viewing pleasure, we recorded a video to guide you through the drop:
For any application, it’s important to write diagnostic messages to a log. Diagnostic information is invaluable when you want to identify and diagnose problems with your application.
While logging and tracing is in some respects similar, the audience and purpose are quite different:
Tracing is a technique to help developers analyze and diagnose problems with their application. For a developer who has to hunt down a problem, you can never have too many tracing information. However, because the amount of trace information can be overwhelming (for a developer, but also for the system that has to write all the information), tracing levels should be adjustable at runtime.
What should you trace?
Typically, tracing information goes to a log file on the machine that does the tracing.
Logging is a technique to help IT pro’s to detect, diagnose and troubleshoot problems with their applications. Often this information will be aggregated into a central monitoring system like Service Center Operation Manager (SCOM).
Information written to the log should be understandable and actionable by IT Pro’s. A message like “Object reference not set in method Foo()” can be helpful for a developer, but doesn’t really say anything to an IT Pro. However, a message like “Could not connect to database X, the connection timed out.” does make a lot of sense to an IT Pro.
What should you log?
Typically, log information is stored in the EventLog, because this is easily consumed with tools like SCOM. However, some organizations might prefer storing this information in a central database.
In Drop 7, we have built a basic logging and tracing implementation.
The idea of this logging implementation is that you have a logging and tracing implementation that works out of the box. However, if you need more flexibility or have an existing logging implementation, you should be able to plug that in. For example, we’re thinking of supplying an Enterprise Library logging implementation for Sharepoint. If you don’t know or care about EntLib, you shouldn’t have to bite off the extra complexity that entlib brings. But if you need to, you should be able to plug it in easily.
The following code snippet shows how to use the logging API:
1: ILogger logger = new SharepointLogger();
2:
3: // Write a diagnostic statement (just to the ULS)
4: logger.LogDiagnostic("Just executed query: " + camlQuery);
5:
6: // Write a warning and a error message (both to the ULS and the EventLog)
7: logger.LogWarning("Sharepoint list exeeds 2000 items. Listname = " + listName, SHAREPOINT_LIST_EXCEEDED);
8: logger.LogError("Could not connect to database: " + databaseName, DATABASE_CONNECTION_EVENTID);
9:
10: // General purpose API
11: logger.Log("logmessage", TraceSeverity.Verbose, myEventID, "MyCategory")
There are several methods like LogDiagnostic, LogWarning and LogError, that are explicit about the specific log scenario.
For our basic logging implementation we took the following approach:
We decided to use the ULS for our tracing. This allows a developer to see his diagnostic messages in the context of sharepoint’s trace messages. If the developer is trying to diagnose a problem that is caused by an issue in sharepoint, he can more easily find the root cause.
When we are logging to the eventlog, you need to write to a specific Event Source. An Event Source specifies the name of an application that is allowed to write to the eventlog. Unfortunately, the asp.net user account by default doesn’t have the rights to create new event sources. So either you should allow the asp.net user to create the eventsources or you should pre create event sources on all your web servers.
For now, we took the approach of (ab)using the “Office Sharepoint Server” event source, because this should be available out of the box. In a future drop, we probably want to be able to configure the event source and probably the application log name.
In your application code, you often need to have access to system level services. For example, you want to log messages or you want to do some type of data access and need a specific repository. Of course, you can new up the concrete implementation your service in your application code. However, if you want to be able to plug in different services, you’d need to change all the places where you’ve created the services.
A better way is to hide creation of services in a service locator. Instead of using:
SharepointLogger logger = new SharePointLogger() logger.LogError("Wrong!");
Ilogger = new ServiceLocator.Current.GetInstance<ILogger>() logger.LogError("Wrong!");
Now, your application code is no longer tied to any specific implementation of logger.
We have included a very simple ServiceLocator in this drop. It hard codes the coupling between the interfaces and their implementations. Still, this provides a lot of benefits, because the which concrete implementation is used is defined in a single location. The interface of this class was created using the interface from the Common Service Locator project.
In a future release, we might use a different technique. For example, we might build our own configuration based service locator or use Unity and the Common Service Locator for this.
While developing the Reference Implementation, we found a number of places where we needed to know the ID of the currently logged on partner, or the URL to it’s collection. Since this information is stored in a central location, we decided to create a helper class to encapsulate this information.
Currently, the partner site directory has only public static methods, but now that we have a service locator, we can create this as a service behind a interface and use the service locator to find it. We’ll tackle that in a future iteration.
We hope you think the things we have been working on in this release are useful. Let us know what you think.
In the next release, we are looking at tackling (among other things) exception handling.
Keep practicing!
_Erwin