Configuration of the Log Manager is a one time step that should be performed when the application starts. There are two ways that you can do this:
- Automatic and using .config file settings
- Manually by configuring a LogAdapter
Automatic Configuration using .Config File Settings
Using .config file settings is the preferred way to use the LogManager as it allows for non-code based switching of the logging mechanism used. The code to load the LogManager is simple:
// create a log manager based on the LogManager config settings
LogManager.Create();
// Start Logging
WebLogEntry entry = new WebLogEntry()
{
Message = "Application Started",
ErrorLevel = ErrorLevels.Message
};
LogManager.Current.WriteEntry(entry);
Once an instance is created you can using LogManager.Current to access the global static instance of the log manager and write to the log as well as query information out of the log.
To use it you have to set up several configuration settings in your .config file:
Sql Configuration:
<LogManager>
<add key="ConnectionString" value="DevSampleConnectionString" />
<add key="LogFilename" value="ApplicationWebLog" />
<add key="LogAdapter" value="Sql" />
<add key="LogWebRequests" value="True" />
<add key="LogErrors" value="True" />
</LogManager>
Xml Configuration:
<LogManager>
<add key="ConnectionString" value="~/Admin/ApplicationWebLog.xml" />
<add key="LogFilename" value="~/Admin/ApplicationWebLog.xml" />
<add key="LogAdapter" value="Xml" />
<add key="LogWebRequests" value="True" />
<add key="LogErrors" value="True" />
</LogManager>
Note: ConnectionString and LogManager really map to the same thing so only one of them really needs to be set.
You also need to define the configuration section:
<configuration>
<configSections>
<section name="LogManager" requirePermission="false" type="System.Configuration.NameValueSectionHandler,System,Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
</configuration>
This config is based on the AppConfiguration class so the config section and keys can auto-generate the first time you run the app and hit the logging engine assuming your application has rights to write to the .config file.
In the sample application and also in the default project template setup the AppConfiguration.aspx page allows for editing these settings interactively:
ConnectionString
The connection string's format depends on the type of LogAdapter that's used (LogType). For Sql this is a connection string, for XML it can be a filename. For sql strings the connection string can be either a full connection string or an entry from the ConnectionStrings section.
LogFileName
Depends on the logging format in use. If you use SQL this will be the table name data is written to. For Xml this value is ignored or can be the same as the connection string which is a filename.
LogAdapter
The output location where data is sent to. The two supported options currently are Sql (Sql Server) and Xml (Xml File on Disk).
LogWebRequests and LogErrors
The configuration values are not used directly by the log provider but they are provided to let your application decide whether it should log requests and errors. They are meant just as a convenient place to hold logging settings.
Manual Configuration
You can also manually configure the LogManager by explicitly instantiating a LogAdapter and configuring it. This allows for custom options as well as the ability to create a custom LogAdapter for storing data in additional formats.
To instantiate the LogManager you'll want to do it in your application's startup code (here this code is called from Application_Start):
public static void OnApplicationStart()
{
if (App.Configuration.LogErrors == ErrorLogTypes.SqlServer)
{
// Create a new Sql Log Manager and point DevSampleConnectionString in web.config
SqlLogAdapter sqlLogAdapter = new SqlLogAdapter("DevSampleConnectionString");
sqlLogAdapter.Tablename = "ApplicationWebLog";
LogManager.Create(sqlLogAdapter);
WebLogEntry entry = new WebLogEntry()
{
Message = "Application Started",
ErrorLevel = ErrorLevels.Message
};
LogManager.Current.WriteEntry(entry);
}
}
To create an instance of the LogManager you start by creating and configuring LogAdapter. In this case I'm creating the SqlLogAdapter which writes to SQL Server and I'm specifying that it should use a table called ApplicationWebLog. The table is created if it doesn't exist - assuming the account specified in the connection string has rights to do so. Otherwise you can create the table yourself using the Sql Log Data Structure.
See also
Logging Entries© West Wind Technologies, 1996-2016 • Updated: 12/19/15
Comment or report problem with topic