NLog 在Asp.NET MVC5中使用文件和数据库设置
来源:互联网 发布:鹏博士数据 编辑:程序博客网 时间:2024/04/30 10:29
1.添加NLog的引用
在程序包管理控制平台中执行 Install-Package NLog
2.添加NLog的配置,在项目中添加Web配置文件输入文件名,NLog.config
3.编写配置文件, 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="false"
throwExceptions="true"> // 注意此处要是false, 当日志发生异常程序不会发生异常,对于调试来说不适用。项目发布时选择false,较妥当。
<targets>
<target xsi:type ="File" //文件设置
name="file"
fileName="${basedir}/App_Data/Logs/${shortdate}.log"
layout="${longdate} - ${level:uppercase=true}:${message} ${callsite:fileName=true} ${exception:format=Type,Message,Method,StackTrace:maxInnerExceptionLevel=5:innerFormat=ShortType,Message,Method,StackTrace}"
keepFileOpen="false"
archiveFileName="${basedir}/App_Data/Logs/Backup_${shortdate}.{##}.log"
archiveNumbering="Sequence"
archiveEvery="Day"
maxArchiveFiles="30">
</target>
<target xsi:type="Database" //数据库设置
name="database"
dbProvider="Npgsql.NpgsqlConnection,Npgsql,Version=3.0.1.0,Culture=neutral,PublicKeyToken=5d8b90d52f46fda7" //注意此处必须这么填写,否则会提示创建连接异常
connectionString="Server=localhost;Database=onlinetest;User Id=otadmin;Password=otadmin;"> //链接字符串
<commandText> //添加SQL
INSERT INTO log(event_time, level, logger, message, exce_type, operation,exce_message, stacktrace, username) VALUES (now(), @level, @logger, @message, @exce_type, @operation,@exce_message, @stacktrace, @username);
</commandText>
<parameter name="@level" layout="${level}" /> //参数值映射
<parameter name="@logger" layout="${logger}" />
<parameter name="@message" layout="${message}" />
<parameter name="@exce_type" layout="${exception:format=type}" />
<parameter name="@operation" layout="${exception:format=method}" />
<parameter name="@exce_message" layout="${exception:format=message}" />
<parameter name="@stacktrace" layout="${exception:format=stacktrace}" />
<parameter name="@username" layout="${identity}" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Warn" writeTo="file" />
<logger name="*" minlevel="Debug" maxlevel="Info" writeTo="database" />
</rules>
</nlog>
说明:要是将上面的配置拷贝后直接可用必须将//后面的注释删除否则会不正确。
参数映射Postgres数据库datetime类型字段提示错误。没有解决。
- NLog 在Asp.NET MVC5中使用文件和数据库设置
- NLog 在winform和asp.net下使用快速攻略
- NLog 在winform和asp.net下使用快速攻略
- NLog 在winform和asp.net下使用快速攻略 .
- asp.net mvc5中spring.net的配置和使用
- ASP.NET Core 开发-Logging 使用NLog 写日志文件
- 译:在ASP.NET MVC5中如何使用Apache log4net 类库来记录日志
- 在ASP.NET MVC5中如何使用Apache log4net 类库来记录日志
- 在ASP.NET MVC5应用程序中快速接入QQ和新浪微博OAuth
- 在ASP.NET MVC5中实现具有服务器端过滤、排序和分页的GridView
- ASP.NET Core 和 NLog 集成
- asp.net mvc5中使用缓存依赖SqlCacheDependency
- asp.net mvc5中使用缓存依赖SqlCacheDependency
- .NET中使用NLog记录日志
- ASP.NET在使用Access数据库的IIS和目录权限设置
- 在MVC5中使用Ninject
- asp.net MVC5 sitemap 的使用
- ASP.NET MVC5使用Area区域
- contiki for CC2530的IAR移植
- 冒泡排序Bubble Sort
- myeclipse中解决 java heap space 的方法
- 【JAVA】.jar与sources.jar浅谈
- Hibernate执行sql语句
- NLog 在Asp.NET MVC5中使用文件和数据库设置
- 递归函数
- android获取设备IMEI ,手机名称,系统SDK版本号,系统版本号
- DEV-C++官方下载地址
- STM32F4——定时器原理及应用(中断、输入捕获、PWM输出)
- gdb调试的layout使用
- UVa 1303 - Wall(凸包)
- 一条忍着不死的鱼
- Ubuntu14搭建ML开发环境