Yes this is possible, see this MSDN article: http://msdn2.microsoft.com/hi-in/library/aa979595(en-us).aspx
And here is some sample code in C#:
using System;
using System.Runtime.InteropServices;
using Microsoft.SharePoint.Administration;
namespace ManagedTraceProvider
{
class Program
{
static void Main(string[] args)
{
TraceProvider.RegisterTraceProvider();
TraceProvider.WriteTrace(0, TraceProvider.TraceSeverity.High, Guid.Empty, "MyExeName", "Product Name", "Category Name", "Sample Message");
TraceProvider.WriteTrace(TraceProvider.TagFromString("abcd"), TraceProvider.TraceSeverity.Monitorable, Guid.NewGuid(), "MyExeName", "Product Name", "Category Name", "Sample Message");
TraceProvider.UnregisterTraceProvider();
}
}
static class TraceProvider
{
static UInt64 hTraceLog;
static UInt64 hTraceReg;
static class NativeMethods
{
internal const int TRACE_VERSION_CURRENT = 1;
internal const int ERROR_SUCCESS = 0;
internal const int ERROR_INVALID_PARAMETER = 87;
internal const int WNODE_FLAG_TRACED_GUID = 0x00020000;
internal enum TraceFlags
{
TRACE_FLAG_START = 1,
TRACE_FLAG_END = 2,
TRACE_FLAG_MIDDLE = 3,
TRACE_FLAG_ID_AS_ASCII = 4
}
// Copied from Win32 APIs
[StructLayout(LayoutKind.Sequential)]
internal struct EVENT_TRACE_HEADER_CLASS
{
internal byte Type;
internal byte Level;
internal ushort Version;
}
// Copied from Win32 APIs
[StructLayout(LayoutKind.Sequential)]
internal struct EVENT_TRACE_HEADER
{
internal ushort Size;
internal ushort FieldTypeFlags;
internal EVENT_TRACE_HEADER_CLASS Class;
internal uint ThreadId;
internal uint ProcessId;
internal Int64 TimeStamp;
internal Guid Guid;
internal uint ClientContext;
internal uint Flags;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct ULSTraceHeader
{
internal ushort Size;
internal uint dwVersion;
internal uint Id;
internal Guid correlationID;
internal TraceFlags dwFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
internal string wzExeName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
internal string wzProduct;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
internal string wzCategory;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 800)]
internal string wzMessage;
}
[StructLayout(LayoutKind.Sequential)]
internal struct ULSTrace
{
internal EVENT_TRACE_HEADER Header;
internal ULSTraceHeader ULSHeader;
}
// Copied from Win32 APIs
internal enum WMIDPREQUESTCODE
{
WMI_GET_ALL_DATA = 0,
WMI_GET_SINGLE_INSTANCE = 1,
WMI_SET_SINGLE_INSTANCE = 2,
WMI_SET_SINGLE_ITEM = 3,
WMI_ENABLE_EVENTS = 4,
WMI_DISABLE_EVENTS = 5,
WMI_ENABLE_COLLECTION = 6,
WMI_DISABLE_COLLECTION = 7,
WMI_REGINFO = 8,
WMI_EXECUTE_METHOD = 9
}
// Copied from Win32 APIs
internal unsafe delegate uint EtwProc(NativeMethods.WMIDPREQUESTCODE requestCode, IntPtr requestContext, uint* bufferSize, IntPtr buffer);
// Copied from Win32 APIs
[DllImport("advapi32.dll", CharSet = CharSet.Unicode)]
internal static extern unsafe uint RegisterTraceGuids([In] EtwProc cbFunc, [In] void* context, [In] ref Guid controlGuid, [In] uint guidCount, IntPtr guidReg, [In] string mofImagePath, [In] string mofResourceName, out ulong regHandle);
// Copied from Win32 APIs
[DllImport("advapi32.dll", CharSet = CharSet.Unicode)]
internal static extern uint UnregisterTraceGuids([In]ulong regHandle);
// Copied from Win32 APIs
[DllImport("advapi32.dll", CharSet = CharSet.Unicode)]
internal static extern UInt64 GetTraceLoggerHandle([In]IntPtr Buffer);
// Copied from Win32 APIs
[DllImport("advapi32.dll", SetLastError = true)]
internal static extern uint TraceEvent([In]UInt64 traceHandle, [In]ref ULSTrace evnt);
}
public enum TraceSeverity
{
Unassigned = 0,
CriticalEvent = 1,
WarningEvent = 2,
InformationEvent = 3,
Exception = 4,
Assert = 7,
Unexpected = 10,
Monitorable = 15,
High = 20,
Medium = 50,
Verbose = 100,
}
public static void WriteTrace(uint tag, TraceSeverity level, Guid correlationGuid, string exeName, string productName, string categoryName, string message)
{
const ushort sizeOfWCHAR = 2;
NativeMethods.ULSTrace ulsTrace = new NativeMethods.ULSTrace();
// Pretty standard code needed to make things work
ulsTrace.Header.Size = (ushort)Marshal.SizeOf(typeof(NativeMethods.ULSTrace));
ulsTrace.Header.Flags = NativeMethods.WNODE_FLAG_TRACED_GUID;
ulsTrace.ULSHeader.dwVersion = NativeMethods.TRACE_VERSION_CURRENT;
ulsTrace.ULSHeader.dwFlags = NativeMethods.TraceFlags.TRACE_FLAG_ID_AS_ASCII;
ulsTrace.ULSHeader.Size = (ushort)Marshal.SizeOf(typeof(NativeMethods.ULSTraceHeader));
// Variables communicated to SPTrace
ulsTrace.ULSHeader.Id = tag;
ulsTrace.Header.Class.Level = (byte)level;
ulsTrace.ULSHeader.wzExeName = exeName;
ulsTrace.ULSHeader.wzProduct = productName;
ulsTrace.ULSHeader.wzCategory = categoryName;
ulsTrace.ULSHeader.wzMessage = message;
ulsTrace.ULSHeader.correlationID = correlationGuid;
// Pptionally, to improve performance by reducing the amount of data copied around,
// the Size parameters can be reduced by the amount of unused buffer in the Message
if (message.Length < 800)
{
ushort unusedBuffer = (ushort) ((800 - (message.Length + 1)) * sizeOfWCHAR);
ulsTrace.Header.Size -= unusedBuffer;
ulsTrace.ULSHeader.Size -= unusedBuffer;
}
if (hTraceLog != 0)
NativeMethods.TraceEvent(hTraceLog, ref ulsTrace);
}
public static unsafe void RegisterTraceProvider()
{
SPFarm farm = SPFarm.Local;
Guid traceGuid = farm.TraceSessionGuid;
uint result = NativeMethods.RegisterTraceGuids(ControlCallback, null, ref traceGuid, 0, IntPtr.Zero, null, null, out hTraceReg);
System.Diagnostics.Debug.Assert(result == NativeMethods.ERROR_SUCCESS);
}
public static void UnregisterTraceProvider()
{
uint result = NativeMethods.UnregisterTraceGuids(hTraceReg);
System.Diagnostics.Debug.Assert(result == NativeMethods.ERROR_SUCCESS);
}
public static uint TagFromString(string wzTag)
{
System.Diagnostics.Debug.Assert(wzTag.Length == 4);
return (uint) (wzTag[0] << 24 | wzTag[1] << 16 | wzTag[2] << 8 | wzTag[3]);
}
static unsafe uint ControlCallback(NativeMethods.WMIDPREQUESTCODE RequestCode, IntPtr Context, uint* InOutBufferSize, IntPtr Buffer)
{
uint Status;
switch (RequestCode)
{
case NativeMethods.WMIDPREQUESTCODE.WMI_ENABLE_EVENTS:
hTraceLog = NativeMethods.GetTraceLoggerHandle(Buffer);
Status = NativeMethods.ERROR_SUCCESS;
break;
case NativeMethods.WMIDPREQUESTCODE.WMI_DISABLE_EVENTS:
hTraceLog = 0;
Status = NativeMethods.ERROR_SUCCESS;
break;
default:
Status = NativeMethods.ERROR_INVALID_PARAMETER;
break;
}
*InOutBufferSize = 0;
return Status;
}
}
}
I was recently tasked to "prototype up some loggin'" for an upcoming project. I didn't have any logging framework experience. I researched, ran through tutorials, made toy apps, etc. on Log4Net, NLog, and Enterprise Library for a few days. Came back 3-4 weeks later and put them together into a cohesive demo. Hopefully some of this is useful to you.
My recommendation for our project is this:
- Use a logging facade (e.g. Common.Logging, SimpleLoggingFacade) to avoid direct dependencies.
- If we end up using Enterprise Library for other facilities, then use it for Logging, too.
- If we end up using something with a dependency on Log4Net, use Log4Net.
- If none of the above, use NLog. Which I'd prefer.
That's based on these findings (opinions!):
- All 3 frameworks are capable and can do some sophisticated things. We want a quality solution, but frankly don't need ultra high performance or 60 types of event sinks.
- All 3 have very similar basic concepts.
- Each has its own cool tricks, like really advanced routing, or dynamic log filenames, file truncating, etc.
- All 3 are pretty well documented in their own way.
- For a complete newb like me, they were all a little awkward initially. No drastic differences here for the basics. I got over it.
- When revisiting things a few weeks later, NLog was clearly the easiest to resume. I needed very little brush up on it. With Log4Net, I had to revisit a few online examples to get going. With EntLib, I gave up and did the tutorials all over again from scratch - I was totally lost.
- I couldn't figure out how to get EntLib to do some things like log to the database. It might be easy, but it was beyond my time limit.
- Log4Net and NLog have a small in-code footprint. EntLib is spammy, but I'd use a facade over it anyway.
- I accidentally mis-configured EntLib and it told me at run time. Log4Net didn't. I didn't have an accidental mis-config with NLog.
- EntLib comes with a nice looking app.config editor, which you 100% need. NLog has a config file schema so you get "intellisense". Log4Net comes with nada.
So obviously I like NLog so far. Not enough to use it in spite of having another solution available, though.
Best Answer
I implemented this recently and came up with a solution that worked for me.
Deploy your log4net config file to the 12 hive and the log4net dll into the GAC using a globally scoped solution. Then in your application code explicitly initialize log4net from the location of your global file. This allows you to log feature receiver, timer jobs and web application code.
see here http://www.codeproject.com/KB/sharepoint/SharepointLog4Net.aspx