vrijdag 17 september 2010

SharePoint 2010 Logging to the ULS

Two days ago I started creating a small but effective logging component for SharePoint 2010. I made one for 2007 that could write to the ULS log (well got it mainly from my friend google) and I reaqd its much easier in the 2010 version of SharePoint. And it is, the older code could go out the door and the new way of handling logging only requires around 20 lines of code for a simple custom implementation.

I took a sample from somewhere that showed how to implement the DiagnosticsServiceBase, which was already having all the functionality I wanted. The code below is roughly the minimum you are required to write to get started.

[Guid("70B3CC24-E48E-4C02-9273-4BC6B6320AB7")]

public class MyDiagnosticsService : SPDiagnosticsServiceBase

{

public const string AreaName = "Virtual Affairs";

public MyDiagnosticsService()

{

}

public MyDiagnosticsService(string name, SPFarm parent)

: base(name, parent)

{

}

public static void WriteToLog(string category, string message, TraceSeverity? severity)

{

SPDiagnosticsCategory diagnosticsCategory = MyDiagnosticsService.Current.Areas[AreaName].Categories[category];

if (severity == null)

{

MyDiagnosticsService.Current.WriteTrace(0, diagnosticsCategory, diagnosticsCategory.DefaultTraceSeverity, message);

}

else

{

MyDiagnosticsService.Current.WriteTrace(0, diagnosticsCategory, (TraceSeverity)severity, message);

}

}

protected override IEnumerable ProvideAreas()

{

List categories = new List();

categories.Add(new SPDiagnosticsCategory(category, TraceSeverity.Unexpected, EventSeverity.Error));

List areas = new List

{

new SPDiagnosticsArea(AreaName, categories)

};

return areas;

}

}

Now I wanted to have my own categories, else I could just use the default DiagnosticsService, and have the Prduct column in the ULS populated with my product name. Now you get there by creating a similar class as shown above.

Now that is all working pretty well I wanted to have some standard categories in stead of having other developers just typing, or mistyping, their own. As the categories are predefined I don’t like to use string as they might be wrong. So I added a enum Category in the class so you can use one of those. I modified the WriteToLog so it takes a Category instead of a string and used the Enum.GetName() to get the string representation of the enum.

Now I was faced with the only thing I could just not get some decent info on and that was how I get it to show up in the Central Admin monitoring pages, specially the event throttling page. You do need to enable site administrators to set the level of detail they want to end up in the ULS log. Registering was mentioned and DiagnosticsService.Update() (in a powershell description on the msdn site). Also the mention of having to add a Registry key, which; a) I find not do professional in a delivery process and b) should be done on each web front in the farm (I guessed).

Funny enough I got it working in a farm level feature by calling MyDiagnosticsService.Update() and MyDiagnosticsService.Delete(). I had some troubles testing this, things got stuck and I only got twice the categories. But after I recreated a new SharePoint farm and added the solution I could turn on and of the Diagnostics area and categories. That is they appeared and disappeared when I expected them to.

So in a nutshell, to create custom logging to the ULS:

1. Implement a class based on DiagnosticsServiceBase

2. Use an enumeration to circumvent category naming typos and exceptions

3. Use the Update() and Delete() methods on your custom diagnostics class to register the area and categories.

Or, which I found out after browsing and searching, use the SharePoint Logger component which is part of a SharePoint Guidance toolkit. Found that too late and it was a bit overwhelming, have to evaluate that package when I have some free time to spend. Link is here:

http://www.microsoft.com/downloads/en/details.aspx?FamilyID=64b55569-2168-4545-8b7c-f185b2cf967d&displaylang=en

If you have a bit of free time try to find it yourself. The download is SharePointGuidance2010. Try that name or SharePoint Guidance, I found it by sheer luck I guess.

And some other links I based my work on are:

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

http://spg.codeplex.com/ (annoying one cause it made me think I could download the code there)

http://msdn.microsoft.com/en-us/library/ff798371.aspx (fun and games from msdn, tells you all about it except where you get it => annoying aswell)

dinsdag 14 september 2010

Simple site deployment when you don't need all the fuzz

Hi all I'd like to share a simplistic way of deploying a made on your dev server solution to a production-like environment.

Yesterday I was working on a project for a simple Intranet in which we wanted to use default Mcrosoft stuff, create a site structure and add some lists and webparts to pages. This should be deployed at the customers environment as sort of pilot. My initial thought were, why not install the thing in the customers environment and do the work there. Well as it turns out the environment isn't ready yet, so I was looking for an alternative.

The consultant on the job wanted to do a save site with content, the thing consultants liked to do using MOSS 2007. Well as a developer, I'm ofcourse not following that path, its for hobbyist, costs (however less than the 2003 version) performance and as it turns out, doesn't work out of the box for publishing sites in SharePoint 2010. You can with a hack, but I guess it will bite you when in production, or other times you least expect it. Usually (not always) things are eft out for a reason.

So what's the aternative. Making the templates, making a manifested template with all the sites, add al modificatons in the templates, nah too much tidous work. Making a backup and later resore this, that might work, however I'm not doing the installation and (haven't tested this yet) it might get to complex with SQL, permissions and all. But while investating if ts even possible, I spotted with my little eye: "Export a site or list". Now that seems to easy so were going to try this out. Funny enough there's no "Import a site or list" function, you need Powershell for that (even better now I can get all devvie about it).

So I:
1. Created a site structure containing several sites
2. Used the epxort to export the sites
3. Removed them all and recreated the webapp
4. Created an initial site collecton using the same template as the root site in the export had
5. Started a powershell prompt
6. Ran Import-SPWeb with the correct url and the reference to the exported file
7. Voila all sites returned like before I exported them

Now this was just an initial test and it seems to work. I can image that when there are custom features required they need to be available at Farm and Site Collection level. But still this is a nice method for deploying pre made sites into another environment, you can make a script that installs the packages required, activate any necessary features and import the structure.

Hope this helps anyone struggling with how to deploy all the work done on a dev server and make it a happy experience.

woensdag 30 juni 2010

Introduction

Hi internet surfers. This is my personal blog, so who am I?

My name is Remco Bosman and I'm just your middle of the run IT guy doing different kinds of things, like; C# programming, Microsoft CRM, Microsoft SharePoint (lots of Microsoft), but also following the XBMC progress as its my main media center, Windows Home Server as thats my main storage and backup facilitator and every now and then I like to play a (low stakes) poker tournament.

I am working for a company called Virtual Affairs in the Netherlands. We are focusing on Microsoft solutions and primarily web based applications. Hence the CRM and SharePoint points above.

Well we'll see what what I have to offer as a blogger!