Log System

DbEntry provides a simple log system.

The log recorder could be defined in the configuration file.

There are some pre-defined recorders.
  • DebugLogRecorder will output the information to debug window in Visual Studio.
  • ConsoleLogRecorder will output the information to console.
  • TextFileLogRecorder will output the information to a text file.
  • CacheTextFileLogRecorder will output the information to a text file by cache it in RAM for 10 secends at first.
  • DtsFileLogRecorder will output the information to a text file with dts format. So we can import it to database later.
  • DatabaseLogRecorder will output the information to a database table named Log, it could be created by auto create table feature.
In Logger, there are some pre-defined loggers in it. Such as SQL, Default and System.

The ORM part is using SQL logger to log composed SQLs. So we can define a log recorder to log all ORM composed sql to analyze.

The following shows how to use it:

Edit the App.config as following:

<?xml version="1.0" encoding="utf-8" ?>
    <section name="Leafing.Settings"
        type="Leafing.Util.Setting.NameValueSectionHandler, Leafing.Util" />

    <add key="SqlLogRecorder" value="@Console" />
    <add key="DefaultLogRecorder" value="@Console" />
    <add key="SystemLogRecorder" value="@Console" />

Log recorder value starts with @ will be consider as pre-definded recorder. Use a self-defined recorder need set the value as the FULLNAME of the recorder type.

And we can use | to set more log recorder(s) in config file as:

<add key="DefaultLogRecorder" value="@Console | @Debug" />

Edit the Program.cs as following:

using System;
using Leafing.Util.Logging;

class Program
    static void Main(string[] args)
        Logger.SQL.Trace("test 0");
        Logger.Default.Trace("test 1");
        Logger.Default.Warn("test 2");
        Logger.System.Error("test 3");

The result is:

Trace,Program.Main(string[] args),SQL,test 0,
Trace,Program.Main(string[] args),Default,test 1,
Warn,Program.Main(string[] args),Default,test 2,
Error,Program.Main(string[] args),System,test 3,

Last edited Apr 9, 2014 at 2:31 PM by lifeng, version 13


culmencasa Jan 27, 2015 at 5:11 AM 
4.2 改成这个了
<section name="Leafing.Settings" type="Leafing.Core.Setting.NameValueSectionHandler, Leafing.Core" />

shiningrise Jun 5, 2012 at 3:50 AM 
自定义log文件: <add key="LogFileName" value="{0}\Logs\{1}.{2}.log" />

shiningrise Jun 5, 2012 at 3:50 AM 
<add key="@Leafing_Log" value="System_Log" />

<add key="SqlLogRecorder" value="@ConsoleMessage | Leafing.UnitTest.SqlRecorder, Leafing.UnitTest" />

<add key="UnitLogRecorder" value=" @DtsFile | @Console"/>
<add key="Unit2LogRecorder" value="@ConsoleMessage"/>


@Console 输出全部字段,@ConsoleMessage 只输出 Message 字段。