记录应用程序块

时间:2009-08-14 15:05:36

标签: c# security sharepoint logging enterprise-library

我在ASP.NET应用程序中使用了日志记录应用程序块,并希望将应用程序转换为Sharepoint WebPart。

只要我改变,一切正常:

<trust level="WSS_Minimal" originUrl="" />

to

<trust level="Full" originUrl="" />

如果不是,我在日志中得到例外:

Failed to add webpart *************255Fcatalogs%252Fwp%252FSearchWebPart%252Ewebpart;SearchWebPart.  Exception Microsoft.SharePoint.WebPartPages.WebPartPageUserException: The type initializer for 'Microsoft.Practices.EnterpriseLibrary.Logging.Logger' threw an exception. ---> System.TypeInitializationException: The type initializer for 'Microsoft.Practices.EnterpriseLibrary.Logging.Logger' threw an exception. ---> System.TypeInitializationException: The type initializer for 'Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource' threw an exception. ---> System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neut...    
...ral, PublicKeyToken=b77a5c561934e089' failed.     at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)     at System.Security.CodeAccessPermission.Demand()     at System.AppDomainSetup.VerifyDir(String dir, Boolean normalize)     at System.AppDomainSetup.get_ConfigurationFile()     at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSourceImplementation..ctor(Boolean refresh)     at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource..cctor()  The action that failed was:  Demand  The type of the first permission that failed was:  System.Security.Permissions.FileIOPermission  The first permission that failed was:  <IPermission class="System.Security.Permissions.FileIOPermi...   
...ssion, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"  version="1"  PathDiscovery="D:\Inetpub\wwwroot\wss\VirtualDirectories\8686\web.config"/>    The demand was for:  <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"  version="1"  PathDiscovery="D:\Inetpub\wwwroot\wss\VirtualDirectories\8686\web.config"/>    The granted set of the failing assembly was:  <PermissionSet class="System.Security.PermissionSet"  version="1">  <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"  version="1"  Flags="Execution"/>  <IPermission class="System.Security.Permissions.StrongNameIdentityPermis...   
...sion, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"  version="1"  PublicKeyBlob="0024000004800000940000000602000000240000525341310004000001000100257FAE170ABB2AB4A8EF914DFEA757F7DB8C221F01850FC8753A4C6585C0B07749DA33DF4D64A721A070E7CDCDEFC8C786E3626418389BCF461E4300E6F4C477BE5CE64AD12C29D517208D6BA627D9F73A9066B7638BE1FEE3EABE6C3E537B546CB3B5DE5E436F95278BB1E9DBDE85C2A6B624010A8073841D467CC7A0A0C6C8"  Name="Microsoft.Practices.EnterpriseLibrary.Common"  AssemblyVersion="3.1.0.0"/>  <IPermission class="System.Security.Permissions.UrlIdentityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"  version="1"  Url="file:///D:/Inetpub/wwwroot/wss/VirtualDirectories/8686/bin/Microsoft.Practices.EnterpriseLibrary.Common.DLL"/>  <IPe...   
...rmission class="System.Security.Permissions.ZoneIdentityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"  version="1"  Zone="MyComputer"/>  <IPermission class="System.Web.AspNetHostingPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"  version="1"  Level="Minimal"/>  <IPermission class="Microsoft.SharePoint.Security.WebPartPermission, Microsoft.SharePoint.Security, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"  version="1"  Connections="True"/>  </PermissionSet>    The assembly or AppDomain that failed was:  Microsoft.Practices.EnterpriseLibrary.Common, Version=3.1.0.0, Culture=neutral, PublicKeyToken=a646907c4a695009  The Zone of the assembly that failed was:  MyComputer  The Url of the assem...   
...bly that failed was:  file:///D:/Inetpub/wwwroot/wss/VirtualDirectories/8686/bin/Microsoft.Practices.EnterpriseLibrary.Common.DLL     --- End of inner exception stack trace ---     at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.SystemConfigurationSource..ctor()     at Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceFactory.Create()     at Microsoft.Practices.EnterpriseLibrary.Logging.Logger..cctor()     --- End of inner exception stack trace ---     at Microsoft.Practices.EnterpriseLibrary.Logging.Logger.Write(LogEntry log)     at com.okana.sharepoint.SearchWebPart.OnLoad(EventArgs e)     at System.Web.UI.Control.LoadRecursive()     at System.Web.UI.Control.AddedControl(Control control, Int32 index)     at System.Web.UI.ControlCollection.Add(Con...   
...trol child)     at System.Web.UI.WebControls.WebParts.WebPartManager.WebPartManagerControlCollection.AddWebPartHelper(WebPart webPart)     at System.Web.UI.WebControls.WebParts.WebPartManager.WebPartManagerControlCollection.AddWebPart(WebPart webPart)     at System.Web.UI.WebControls.WebParts.WebPartManager.AddWebPart(WebPart webPart)     at System.Web.UI.WebControls.WebParts.WebPartManagerInternals.AddWebPart(WebPart webPart)     at Microsoft.SharePoint.WebPartPages.SPWebPartManager.AddWebPartWithRetry(WebPart webPart)     at Microsoft.SharePoint.WebPartPages.SPWebPartManager.AddDynamicWebPart(WebPart webPart)     at Microsoft.SharePoint.WebPartPages.SPWebPartManager.LoadWebPart(WebPart aspWebPart, String zoneId, Int32 zoneIndex, Boolean isClosed)     at Microsoft.SharePoint.WebPartPages....   
...SPWebPartManager.AddWebPartInternalShared(WebPart webPart)     at Microsoft.SharePoint.WebPartPages.SPWebPartManager.AddWebPartInternal(SPSupersetWebPart superset, Boolean throwIfLocked)     --- End of inner exception stack trace ---     at Microsoft.SharePoint.WebPartPages.SPWebPartManager.AddWebPartInternal(SPSupersetWebPart superset, Boolean throwIfLocked)     at Microsoft.SharePoint.WebPartPages.SPWebPartManager.AddWebPartInternal(SPSupersetWebPart superset)     at Microsoft.SharePoint.WebPartPages.WebPartQuickAdd.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)

我的日志配置如下:

<loggingConfiguration name="Logging Application Block" tracingEnabled="true" defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">
    <listeners>
      <add fileName="XAE.log" rollSizeKB="0" timeStampPattern="yyyy-MM-dd" rollFileExistsBehavior="Overwrite" rollInterval="Day" formatter="Text Formatter" header="" footer="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral" traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral" name="Rolling Flat File Trace Listener" />
      <add fileName="IDOL.log" rollSizeKB="0" timeStampPattern="yyyy-MM-dd" rollFileExistsBehavior="Overwrite" rollInterval="Day" formatter="Text Formatter" header="" footer="" listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.RollingFlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral" traceOutputOptions="None" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.RollingFlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral" name="IDOL Rolling Flat File Trace Listener" />
    </listeners>
    <formatters>
      <add template="{timestamp(local)} : {category} : {message}" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=3.1.0.0, Culture=neutral" name="Text Formatter" />
    </formatters>
    <categorySources>
      <!--
      For any log entries that you wish to suppress set "All" to "Off"
      -->
      <add switchValue="All" name="Communication">
        <listeners>
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </add>
      <add switchValue="All" name="Debug">
        <listeners>
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </add>
      <add switchValue="All" name="Exception">
        <listeners>
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </add>
      <add switchValue="All" name="General">
        <listeners>
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </add>
      <add switchValue="All" name="Warning">
        <listeners>
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </add>
      <add switchValue="All" name="IDOL">
        <listeners>
          <add name="IDOL Rolling Flat File Trace Listener" />
        </listeners>
      </add>
    </categorySources>
    <specialSources>
      <allEvents switchValue="All" name="All Events" />
      <notProcessed switchValue="All" name="Unprocessed Category" />
      <errors switchValue="All" name="Logging Errors &amp; Warnings">
        <listeners>
          <add name="Rolling Flat File Trace Listener" />
        </listeners>
      </errors>
    </specialSources>
  </loggingConfiguration>

显然这是因为它试图创建日志文件而WSS_Minimal不允许这样做。有一种简单的方法可以暂时禁用所有日志记录吗?删除日志记录是有问题的,因为它在底层库中使用。

我已尝试将所有switchValue =“All”设置为“Off”,但即使没有记录任何内容,它仍然会抛出异常

1 个答案:

答案 0 :(得分:4)

如果要使用最小信任,则必须配置代码访问安全策略。查看我在下面写的以下帖子,其中描述了使用Enterprise Library日志记录所需的安全配置。

Enterprise Library in SharePoint