writing to sharepoint log using reflection

Feb 12, 2009 at 10:09 AM
Edited Feb 12, 2009 at 10:13 AM

an alternative is to use reflection. the code below uses the public static method SendTraceTag from the internal static class ULS. More of interest than anything else. The SharePoint ULS Viewer is a much more elegant solution - although I access the viewer from the _layouts/settings.aspx page and have modified the output.



using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Diagnostics; 




public static class Trace
private const string typeName = "Microsoft.Office.Server.Diagnostics.ULS, Microsoft.Office.Server, Version=";
private const string tagName = "Microsoft.Office.Server.Diagnostics.ULSTagID, Microsoft.Office.Server, Version=";
private const string catName = "Microsoft.Office.Server.Diagnostics.ULSCat, Microsoft.Office.Server, Version=";
private const string levelName = "Microsoft.Office.Server.Diagnostics.ULSTraceLevel, Microsoft.Office.Server, Version=";








/// <summary>
/// replacement for using Console.Writeline in deploying
/// </summary>
/// <param name="ulsTagID">tag_7sek</param> 
/// <param name="ulsCat">msoulscat_ULS</param>
/// <param name="ulsTraceLevel">Verbose</param>
/// <param name="message"></param> 
public static void WriteLineToSharePoint(string ulsTagID, string ulsCat, string ulsTraceLevel, string message) 
var type = Type.GetType(typeName, true, false);
var tag = Type.GetType(tagName); 
var cat = Type.GetType(catName);
var level = Type.GetType(levelName);
var types = new[] { tag, cat, level, typeof(string) };
var methodInfo = type.GetMethod("SendTraceTag"BindingFlags.Static | BindingFlags.Public, nullCallingConventions.Standard, types, null);
var enumTag = GetEnumValue(tag, ulsTagID); 
var enumCat = GetEnumValue(cat, ulsCat);
var enumLevel = GetEnumValue(level, ulsTraceLevel);
var constructorParams = new[] { enumTag, enumCat, enumLevel, message };


null, constructorParams); 


private static object GetEnumValue(Type paramName, string paramValue)
var field = paramName.GetField(paramValue);