Search code examples
c#.netsharepointloggingsharepoint-2007

Logging to TextFile from SharePoint


I'm trying to debug a webpart installed on a client's SharePoint instance. I wanted a quick and easy logging feature, so I thought of writing messages to a text file in the temp directory. SharePoint doesn't seem to like it, so what are my options?


Solution

  • I would guess that this is a permissions issue that SharePoint is blocking you on (and probably not telling you that it is). When you try to write to a text file on the server, you need to have elevated permissions in order to do it. You can accomplish this using SPSecurity.RunWithElevatedPrivileges. Something like the following, if you want just a simple, small-code solution.

    SPSecurity.RunWithElevatedPrivileges(delegate() {
        using (StreamWriter sw = new StreamWriter(@"C:\log.txt"))
        {
            //log information here
        }
    });