Logging
To aid in development, LightSpeed can be configured to log diagnostic messages. The logger is any object that implements the ILogger interface: this gives you the flexibility to log according to the requirements of the application and environment. For example, you can log to the Visual Studio Output window during development, then turn off logging in production. Or you could create a custom logger which would allow you to selectively turn on logging for production diagnostics.
Built-In Loggers
The framework provides two built-in loggers:
· ConsoleLogger writes logs to the console window.
· TraceLogger writes logs to all application trace listeners. Trace listeners are defined by the .NET Framework and can be specified in the configuration file via the <system.diagnostics> element. The .NET trace infrastructure also supports filtering of messages.
Both built-in loggers log both SQL and debug messages.
Enabling Logging
To enable logging, set LightSpeedContext.Logger in code, or the loggerClass attribute in configuration. When setting the logger class in configuration, you must provide a full assembly‑qualified type name.
Enabling logging in configuration |
<add name="Test" |
Enabling logging in code |
_context.Logger = new TraceLogger(); |
You can display additional logging information by setting LightSpeedContext.VerboseLogging. This is not available in configuration because it should be set only during debugging.
Disabling Logging
To disable logging after it has been turned on, set LightSpeedContext.Logger to null.
_context.Logger = null; |
Building a Custom Logger
In addition to the built-in loggers, you can create your own custom logger. You could do this for example to integrate with your application’s wider logging infrastructure, to take advantage of logging platforms such as log4net, or to support finer control over logging. To create a custom logger, implement the ILogger interface.
ILogger has two methods: LogSql and LogDebug. LightSpeed calls LogSql for each SQL statement or batch sent to the database. It calls LogDebug primarily for performance-oriented messages such as cache hits and misses and database access times.
LogSql and LogDebug are declared as receiving values of type object. The exact data that LightSpeed passes to these methods may change, but you can safely call ToString on these objects to get readable log text. In the LogSql method, you may also attempt to cast the object to CommandLog. If the object is a CommandLog then you can access individual properties in order to log them individually or log them in a more structured way such as to a database.
A custom logger which uses the application’s logging infrastructure |
public class IntegratedLogger : ILogger |
Note that the VerboseLogging setting does not affect what information is available in the CommandLog object. CommandLog always contains full logging information. VerboseLogging affects only how much information is included in CommandLog.ToString().
Column Name Logging
For each logged data parameter the column name is also logged.