views:

183

answers:

3

I am trying to implement some logging in my webpart. I implemented a Custom trace provider implementation which writes Log messages into the 12 hive logs as has been described here:

http://msdn.microsoft.com/en-us/library/aa979522.aspx

I have wrapped the above code into a dll called logging.DLL.

I am referencing this DLL in my webpart.

I am calling the RegisterTraceProvider in the constructor using elevated privileges.

I have declared the Logging DLL as a safe control in the manifest.xml.

But When i try to add the webpart to the page, I get a security exception with message "Request failed." THis error is thrown in the constructor when it tries to call the RegisterTraceProvider method.

Am I missing something here? How can I get this logging to work?

Edit:Both my logging DLL and my webpart DLL are in the GAC.

+1  A: 

Is your webpart deployed in the GAC or Bin folder? If it is in the Bin folder, the webpart is not running under Full Trust, so you need to write a Code Access Security Policy for your webpart, allowing it to run call's to the API under Full Trust.

for an example check this out or just Google for Sharepoint + Code Access Security Policies.

Colin
+3  A: 

Since the trace provider uses unmanaged code you should mark the method with:

[SecurityPermission(SecurityAction.Assert, SecurityPermissionFlag.UnmanagedCode)]

This will make sure .NET's security stops checking further in the call stack when this attribute is reached, which allow less trusted code to do unmanaged calls thorugh it. Remember that the logging dll still need permissions to run, so either give it CAS-policies in the manifest.xml or put it in the GAC. If you put it in the GAC, mark it with the following attribute which makes it callable from a non fully trusted dll:

[assembly: AllowPartiallyTrustedCallers]

Then you should be able to call it from a WebPart deployed to the bin directory.

JMD
Hey.. Thanks for your post, i learnt something from it. But both my logging DLL and my webpart DLL are in the GAC. I am still getting the exception.
ashwnacharya
A: 

I was Doing m RegisterTraceprovider() call inside the constructor. Looks like this was the reason it was failing. I moved my RegisterTraceProvider() call into OnInit() override, and it started working!!

ashwnacharya