Prabhu
Prabhu

Reputation: 13335

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?

Upvotes: 1

Views: 2819

Answers (4)

Vivek Kumar
Vivek Kumar

Reputation: 2889

There are few ways of custom logging in sharepoint -

  1. Use SPDiagnosticsService - You may write to the ULS via SPDiagnosticsService class.

  2. Utilize diagnostics.asmx web service -

    SharePointDiagnostics SharePointDiagnosticsObject = new SharePointDiagnostics();
    SharePointDiagnosticsObject.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
    string Response = SharePointDiagnosticsObject.SendClientScriptErrorReport(message, file, line, client, stack, team, originalFile);
    

    For more details on usage of diagnostics.asmx refer the following link -

    https://vivekkumar11432.wordpress.com/2016/09/23/how-to-do-logging-in-uls-from-csom-in-c/

For more details on logging refer the following link -

http://www.codeproject.com/Articles/620996/Five-suggestions-to-implement-a-better-logging-in

Don't use

Microsoft.Office.Server.Diagnostics.PortalLog.LogString("Message");

According to Microsoft documentation - LogString is reserved for internal use and is not intended to be used directly from your code.

Upvotes: 1

John Ptacek
John Ptacek

Reputation: 1886

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.

Upvotes: 1

Grace Note
Grace Note

Reputation: 3215

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
    }
});

Upvotes: 0

kamahl
kamahl

Reputation: 941

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

Upvotes: 0

Related Questions