Open sandboxFocusImprove this doc

Troubleshooting Caching

If you need to troubleshoot the caching aspect, you can enable logging for this feature. The procedure depends on whether you use PostSharp Logging in your application.

Enabling logging of caching with PostSharp Logging

If you use PostSharp Logging for logging, you can enable detailed logging of the caching aspect by enabling the Caching logging role for the whole application or a specific type or namespace.

The following code shows how to log the details of the caching aspect to the system console for the whole application:

LoggingServices.DefaultBackend = new ConsoleLoggingBackend();
LoggingServices.DefaultBackend.DefaultVerbosity.SetMinimalLevel( LogLevel.Debug, LoggingRoles.Caching );

When a cached method is called twice with the same argument, this configuration produces the following output:

Debug     | Cache  | Program | Processing invocation of method CachingLogging.Program.GetNumber(int) | Starting.
Debug     | Cache  |   Program | Processing invocation of method CachingLogging.Program.GetNumber(int) | Key="CachingLogging.Program.GetNumber((int) 1)".
Debug     | Cache  |   MemoryCachingBackend | GetItem( "CachingLogging.Program.GetNumber((int) 1)" ) | Starting.
Debug     | Cache  |   MemoryCachingBackend | GetItem( "CachingLogging.Program.GetNumber((int) 1)" ) | Succeeded: Cache miss.
Debug     | Cache  |   Program | Processing invocation of method CachingLogging.Program.GetNumber(int) | Cache miss: Key="CachingLogging.Program.GetNumber((int) 1)".
Debug     | Cache  |   MemoryCachingBackend | SetItem( "CachingLogging.Program.GetNumber((int) 1)" ) | Starting.
Debug     | Cache  |   MemoryCachingBackend | SetItem( "CachingLogging.Program.GetNumber((int) 1)" ) | Succeeded.
Debug     | Cache  | Program | Processing invocation of method CachingLogging.Program.GetNumber(int) | Succeeded.
Retrieved: 1
Debug     | Cache  | Program | Processing invocation of method CachingLogging.Program.GetNumber(int) | Starting.
Debug     | Cache  |   Program | Processing invocation of method CachingLogging.Program.GetNumber(int) | Key="CachingLogging.Program.GetNumber((int) 1)".
Debug     | Cache  |   MemoryCachingBackend | GetItem( "CachingLogging.Program.GetNumber((int) 1)" ) | Starting.
Debug     | Cache  |   MemoryCachingBackend | GetItem( "CachingLogging.Program.GetNumber((int) 1)" ) | Succeeded: Cache hit.
Debug     | Cache  |   Program | Processing invocation of method CachingLogging.Program.GetNumber(int) | Cache hit: Key="CachingLogging.Program.GetNumber((int) 1)".
Debug     | Cache  | Program | Processing invocation of method CachingLogging.Program.GetNumber(int) | Succeeded.
Retrieved: 1

Enabling logging of caching with System.Diagnostics

If you are not using PostSharp Logging, you can enable logging of the caching aspect by configuring the TraceSource named PostSharp.Cache in your app.config or web.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
	<system.diagnostics>
		<sources>
			<source name="PostSharp.Cache" switchName="PostSharp.Cache" >
				<listeners>
					<add name="console" />
				</listeners>
			</source>
		</sources>
		<switches>
			<add name="PostSharp.Cache" value="-1" />
		</switches>
		<sharedListeners>
			<add name="console" type="System.Diagnostics.ConsoleTraceListener" initializeData="false"/>
		</sharedListeners>
		<trace autoflush="true" indentsize="4">
			<listeners>
				<add name="console" />
			</listeners>
		</trace>
	</system.diagnostics>
</configuration>

See Also

Other Resources

Logging
Getting Started with PostSharp Logging