开发者

Logging to TextFile from SharePoint

开发者 https://www.devze.com 2023-01-02 21:18 出处:网络
I\'m trying to debug a webpart installed on a client\'s SharePoint instance. I wanted a quick and easy 开发者_JAVA技巧logging feature, so I thought of writing messages to a text file in the temp direc

I'm trying to debug a webpart installed on a client's SharePoint instance. I wanted a quick and easy 开发者_JAVA技巧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?


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.


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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消