日志配置文件示例

NLOG

文件源 nlog.config 示例

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
throwExceptions="false"
internalLogLevel="Off" internalLogFile="c:\temp\nlog-internal.log">
<variable name="myvar" value="myvalue"/>
<targets>
<target xsi:type="file" name="File" fileName="${basedir}/logs/${shortdate}.log"
layout="${longdate}||${level}||${logger}||${message}||${exception:format=ToString:innerFormat=ToString:maxInnerExceptionLevel=10:separator=\r\n}||end" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file" />
</rules>
</nlog>

数据库源 nlog.config 示例

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true">
<targets>
<target name="database" xsi:type="Database">
<connectionString>${var:ConnectionString}</connectionString>
<!--
USE [master]
GO
/****** Object: Database [log] Script Date: 2019/1/3 15:06:59 ******/
CREATE DATABASE [log]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'log', FILENAME = N'C:\Users\admin\log.mdf' , SIZE = 8192KB , MAXSIZE = UNLIMITED, FILEGROWTH = 65536KB )
LOG ON
( NAME = N'log_log', FILENAME = N'C:\Users\admin\log_log.ldf' , SIZE = 8192KB , MAXSIZE = 2048GB , FILEGROWTH = 65536KB )
GO
ALTER DATABASE [log] SET COMPATIBILITY_LEVEL = 130
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [log].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [log] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [log] SET ANSI_NULLS OFF
GO
ALTER DATABASE [log] SET ANSI_PADDING OFF
GO
ALTER DATABASE [log] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [log] SET ARITHABORT OFF
GO
ALTER DATABASE [log] SET AUTO_CLOSE OFF
GO
ALTER DATABASE [log] SET AUTO_SHRINK OFF
GO
ALTER DATABASE [log] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [log] SET CURSOR_CLOSE_ON_COMMIT OFF
GO
ALTER DATABASE [log] SET CURSOR_DEFAULT GLOBAL
GO
ALTER DATABASE [log] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [log] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [log] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [log] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [log] SET DISABLE_BROKER
GO
ALTER DATABASE [log] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
GO
ALTER DATABASE [log] SET DATE_CORRELATION_OPTIMIZATION OFF
GO
ALTER DATABASE [log] SET TRUSTWORTHY OFF
GO
ALTER DATABASE [log] SET ALLOW_SNAPSHOT_ISOLATION OFF
GO
ALTER DATABASE [log] SET PARAMETERIZATION SIMPLE
GO
ALTER DATABASE [log] SET READ_COMMITTED_SNAPSHOT OFF
GO
ALTER DATABASE [log] SET HONOR_BROKER_PRIORITY OFF
GO
ALTER DATABASE [log] SET RECOVERY SIMPLE
GO
ALTER DATABASE [log] SET MULTI_USER
GO
ALTER DATABASE [log] SET PAGE_VERIFY CHECKSUM
GO
ALTER DATABASE [log] SET DB_CHAINING OFF
GO
ALTER DATABASE [log] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )
GO
ALTER DATABASE [log] SET TARGET_RECOVERY_TIME = 60 SECONDS
GO
ALTER DATABASE [log] SET DELAYED_DURABILITY = DISABLED
GO
ALTER DATABASE [log] SET QUERY_STORE = OFF
GO
USE [log]
GO
ALTER DATABASE SCOPED CONFIGURATION SET LEGACY_CARDINALITY_ESTIMATION = OFF;
GO
ALTER DATABASE SCOPED CONFIGURATION SET MAXDOP = 0;
GO
ALTER DATABASE SCOPED CONFIGURATION SET PARAMETER_SNIFFING = ON;
GO
ALTER DATABASE SCOPED CONFIGURATION SET QUERY_OPTIMIZER_HOTFIXES = OFF;
GO
USE [log]
GO
/****** Object: Table [dbo].[Log] Script Date: 2019/1/3 15:06:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Log](
[Id] [int] IDENTITY(1,1) NOT NULL,
[MachineName] [nvarchar](50) NOT NULL,
[LongDate] [datetime2] NOT NULL,
[Level] [nvarchar](50) NOT NULL,
[Message] [nvarchar](max) NOT NULL,
[Logger] [nvarchar](250) NULL,
[Callsite] [nvarchar](max) NULL,
[Exception] [nvarchar](max) NULL,
CONSTRAINT [PK_dbo.Log] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
USE [master]
GO
ALTER DATABASE [log] SET READ_WRITE
GO
-->
<commandText>
insert into dbo.Log (
MachineName, LongDate, Level, Message,
Logger, Callsite, Exception
) values (
@MachineName, @LongDate, @Level, @Message,
@Logger, @Callsite, @Exception
);
</commandText>
<parameter name="@MachineName" layout="${machinename}" />
<parameter name="@LongDate" layout="${longDate}" />
<parameter name="@Level" layout="${level}" />
<parameter name="@Message" layout="${message}" />
<parameter name="@Logger" layout="${logger}" />
<parameter name="@Callsite" layout="${callsite}" />
<parameter name="@Exception" layout="${exception:tostring}" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="database" />
</rules>
</nlog>

log4net

数据库源 log4net.config 示例

<?xml version="1.0" encoding="utf-8" ?>
<log4net>
<appender name="RollingFile" type="log4net.Appender.RollingFileAppender">
<file value="LogFiles/"/>
<preserveLogFileNameExtension value="true" />
<datePattern value="yyyy-MM-dd'.log'" />
<staticLogFileName value="false"/>
<appendToFile value="true" />
<maximumFileSize value="100KB" />
<maxSizeRollBackups value="2" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date || %5level || %logger || %message || %exception ||end %newline" />
</layout>
</appender>
<root>
<level value="ALL"/>
<appender-ref ref="RollingFile" />
</root>
</log4net>