How to use Microsoft Enterprise Logging Application block in Medium Trust level?

1) Medium or less trust level does not allow access to the Windows Event Log (System.Diagnostics.EventLogPermission)

2) In the case where access to the Windows Event log is not allowed, I would recommed we write the Error or Trace logs to a Flat file under the root directory e.g. root\log\Error.log (In medium security the application only has access to the root directory and folders under it)

3) Add a section under the “web.config” file for logginConfiguration as shown below, make sure the requiredPersmission = “false”

<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false"/>

4) Add Flat File Listener under the logginConfiguration section

Logging Application block allows writing to multiple log destinations (e.g. Flat File, Sql, Windows Event Log etc). Under Medium trust you will be able to write to a flat file under the application root directory.

<loggingConfiguration name="Logging Application Block" tracingEnabled="true"

defaultCategory="General" logWarningsWhenNoCategoriesMatch="true">

<listeners>

<add name="Flat File Destination"

type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FlatFileTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging"

listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FlatFileTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging"

fileName ="log\Error.log"

header="----------------header------------------------"

footer="----------------footer------------------------"

formatter="Text Formatter"

/>

listeners>

<formatters>

<add template="Timestamp: {timestamp} Message: {message} Category: {category} Priority: {priority} EventId: {eventid} Severity: {severity} Title:{title} Machine: {machine} Application Domain: {appDomain} Process Id: {processId} Process Name: {processName} Win32 Thread Id: {win32ThreadId} Thread Name: {threadName} Extended Properties: {dictionary({key} - {value} )}"

type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

name="Text Formatter" />

formatters>

<categorySources>

<add switchValue="All" name="General">

<listeners>

<add name="Flat File Destination" />

listeners>

add>

categorySources>

<specialSources>

<allEvents switchValue="All" name="All Events" />

<notProcessed switchValue="All" name="Unprocessed Category" />

<errors switchValue="All" name="Logging Errors & Warnings">

<listeners>

<add name="Flat File Destination" />

listeners>

errors>

specialSources>

loggingConfiguration>

Comments

Popular posts from this blog

WPF How to Dispose ViewModel when the associated UserControl (Not Window) closes?

C# How to unit test Dispatcher

WPF: How to Deep Copy WPF object (e.g. UIElement) ?