views:

48

answers:

3

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?

A: 

Try a logging framework like log4net, or write a small logging framework writing into an external database, you could also use lists to log if you want to stay inside sharepoint

Patrick Säuerl
A: 

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
    }
});
ccomet
Hmm, that still does't work...it says SP doesn't have access to the C:
Prabhu
I ended up using Microsoft.Office.Server.Diagnostics.PortalLog.LogString
Prabhu
Firstly if you're using a partially trusted web part then this also depends upon CAS permissions. If its in the GAC then elevating will use the ApplicationPool's identities which will probably not have permission to arbitrary areas of the C drive when setup using domain service accounts (i.e. when setup properly in a farm environment)
Ryan
@Ryan You've a point. I'm too used to working on a dev server when doing my diagnostic/debug logging.
ccomet
A: 

IF you are writing to the temp directory, you will need to give the file (if it exists) or the directory rights for the IIS Application pool that the SharePoint IIS application is running under.

John Ptacek