Logging from webpart to a text file
Hi,
I wanted to log Exceptions and some information from my webpart code to a text file on server's layouts folder. I tried with the code below and got an error. The reason for my writing log to file under layouts folder is, I wanted to view log files from browser using http:// domain name / _layouts/subfolder/filname.txt .
using (StreamWriter swLog = File.AppendText(LogFilePath))
{
swLog.WriteLine(logContent.ToString());
swLog.Flush();
swLog.Close();
}Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.
Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
Any help please..
- Changed TypePeter Jausovec - MSFT Friday, November 06, 2009 4:46 PM
- Moved byPeter Jausovec - MSFT Friday, November 06, 2009 6:17 PMMoving to appropriate forum. (From:SharePoint Development)
Answers
- Hi Kiran J,you probably don't have write permissions in this path.You could use SPSecurity.RunWithElevatedPriviliges (not sure it will work though) or do a more clean logging:using the Microsoft Enterprise Library (needs some config) or use your own logging logic that logs to the Event log.Greets,Frank
- Marked As Answer byCharlie WuModeratorFriday, November 13, 2009 9:56 AM
Hi Kiran,
Never try to log into a file using these method, as it may cause conflicts and locks between different threads for example, multiple users displaying the webpart he very same time.
I usually use the System.Diagnostics.Trace class and its methods (for example, TraceInformation http://msdn.microsoft.com/en-us/library/system.diagnostics.trace.traceinformation.aspx) to create output from your feature receiver. So one can use the WinDbg (http://msdn.microsoft.com/en-us/library/cc266321.aspx, http://windbg.dumpanalysis.org/) to catch the output.You can configure your web application to redirect trace output to file (see <listeners> Element for <trace> http://msdn.microsoft.com/en-us/library/zh3a61fw.aspx, and Trace Listeners http://msdn.microsoft.com/en-us/library/aa983739(VS.71).aspx)
You can even write your log messages to SharePoint Unified Logging System as described here:
SharePoint: Writing messages to ULS (Unified Logging System)
http://weblogs.asp.net/gunnarpeipman/archive/2009/02/25/sharepoint-writing-messages-to-uls-unified-logging-system.aspx
An alternative option is to use custom logging solutions like Log4Net (http://log4net.sourceforge.net/).
Hope that helps.
Peter- Marked As Answer byCharlie WuModeratorFriday, November 13, 2009 9:56 AM
All Replies
- Hi Kiran J,you probably don't have write permissions in this path.You could use SPSecurity.RunWithElevatedPriviliges (not sure it will work though) or do a more clean logging:using the Microsoft Enterprise Library (needs some config) or use your own logging logic that logs to the Event log.Greets,Frank
- Marked As Answer byCharlie WuModeratorFriday, November 13, 2009 9:56 AM
Hi Kiran,
Never try to log into a file using these method, as it may cause conflicts and locks between different threads for example, multiple users displaying the webpart he very same time.
I usually use the System.Diagnostics.Trace class and its methods (for example, TraceInformation http://msdn.microsoft.com/en-us/library/system.diagnostics.trace.traceinformation.aspx) to create output from your feature receiver. So one can use the WinDbg (http://msdn.microsoft.com/en-us/library/cc266321.aspx, http://windbg.dumpanalysis.org/) to catch the output.You can configure your web application to redirect trace output to file (see <listeners> Element for <trace> http://msdn.microsoft.com/en-us/library/zh3a61fw.aspx, and Trace Listeners http://msdn.microsoft.com/en-us/library/aa983739(VS.71).aspx)
You can even write your log messages to SharePoint Unified Logging System as described here:
SharePoint: Writing messages to ULS (Unified Logging System)
http://weblogs.asp.net/gunnarpeipman/archive/2009/02/25/sharepoint-writing-messages-to-uls-unified-logging-system.aspx
An alternative option is to use custom logging solutions like Log4Net (http://log4net.sourceforge.net/).
Hope that helps.
Peter- Marked As Answer byCharlie WuModeratorFriday, November 13, 2009 9:56 AM


