赞
踩
- CREATE TABLE [dbo].[Sys_Log](
- [Id] [varchar](50) NOT NULL CONSTRAINT [DF_Sys_Log_Id] DEFAULT (newid()),
- [Application] [varchar](200) NULL,
- [Logger] [varchar](200) NULL,
- [Level] [varchar](50) NULL,
- [Message] [varchar](max) NULL,
- [Exception] [varchar](max) NULL,
- [Callsite] [varchar](200) NULL,
- [CreateDate] [datetime] NULL,
- CONSTRAINT [PK_SYS_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
-
- SET ANSI_PADDING OFF
- GO
-
- EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主键(默认值是newid())' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Sys_Log', @level2type=N'COLUMN',@level2name=N'Id'
- GO
-
- EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'日志所属应用' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Sys_Log', @level2type=N'COLUMN',@level2name=N'Application'
- GO
-
- EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'日志所属类' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Sys_Log', @level2type=N'COLUMN',@level2name=N'Logger'
- GO
-
- EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'日志级别' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Sys_Log', @level2type=N'COLUMN',@level2name=N'Level'
- GO
-
- EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'日志信息' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Sys_Log', @level2type=N'COLUMN',@level2name=N'Message'
- GO
-
- EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'异常信息' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Sys_Log', @level2type=N'COLUMN',@level2name=N'Exception'
- GO
-
- EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'日志所属方法' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Sys_Log', @level2type=N'COLUMN',@level2name=N'Callsite'
- GO
-
- EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'创建时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Sys_Log', @level2type=N'COLUMN',@level2name=N'CreateDate'
- GO
-
- EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'系统异常日志表' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Sys_Log'
- GO
设置<parameter>节点中的layout="${event-properties:item=callsite}" ,可以自定义日志表中某列的值。
如果设置了layout="${event-properties:item=callsite}",但是没有通过LogEventInfo.Properties[""]对参数赋值,那数据库中本条数据对应的字段值就为空。
- <?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"
- internalLogLevel="Warn"
- internalLogFile="c:\temp\internal-nlog.txt">
-
- <!-- 加载ASP.NET Core插件 -->
- <extensions>
- <add assembly="NLog.Web.AspNetCore"/>
- </extensions>
-
- <!-- 输出目的地 -->
- <targets>
- <!-- 输出到文件,这个文件记录所有日志 -->
- <target xsi:type="File" name="allfile" fileName="d:\temp\nlog-all-${shortdate}.log"
- layout="${longdate} | ${event-properties:item=EventId.Id} | ${logger} | ${uppercase:${level}} | ${message} ${exception}" />
-
- <!-- 另外一个日志记录文件,户口也跳过Microsoft开头相关日志信息 -->
- <target xsi:type="File" name="ownFile-web" fileName="d:\temp\nlog-own-${shortdate}.log"
- layout="${longdate} | ${event-properties:item=EventId.Id} | ${logger} | ${uppercase:${level}} | ${message} ${exception} | ${aspnet-mvc-controller} | ${aspnet-mvc-action}" />
-
- <!-- write to the void aka just remove -->
- <target xsi:type="Null" name="blackhole" />
-
- <target name="database" xsi:type="Database">
- <connectionString>${var:connectionString}</connectionString>
-
- <commandText>
- insert into dbo.Sys_Log (
- Application, CreateDate, Level, Message,Logger, Callsite, Exception
- ) values (
- @Application, @CreateDate, @Level, @Message,@Logger, @Callsite, @Exception
- );
- </commandText>
-
- <parameter name="@application" layout="${event-properties:item=application}" />
- <parameter name="@createDate" layout="${date}" />
- <parameter name="@level" layout="${level}" />
- <parameter name="@message" layout="${message}" />
- <parameter name="@logger" layout="${logger}" />
- <parameter name="@callSite" layout="${event-properties:item=callsite}" />
- <parameter name="@exception" layout="${exception:format=toString,Data}" />
-
- </target>
- </targets>
-
- <!-- 写入目的地的规则 -->
- <rules>
- <!--全部记录,包括Microsoft开头的相关日志信息-->
- <!--<logger name="*" minlevel="Trace" writeTo="allfile" />-->
-
- <!--跳过Microsoft开头的相关日志信息-->
- <logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
- <logger name="*" minlevel="Trace" writeTo="database" />
- <!--<logger name="*" minlevel="Trace" writeTo="ownFile-web" />-->
- </rules>
- </nlog>
1、方法一
步骤一:
- public class Program
- {
- public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
- WebHost.CreateDefaultBuilder(args)
- .UseNLog()//配置NLog(1)
- .UseStartup<Startup>();
- }
步骤二:
- public class Startup
- {
- public Startup(IConfiguration configuration, IHostingEnvironment env)
- {
- Configuration = configuration;
-
- //配置NLog(2)
- env.ConfigureNLog("nlog.config");
- LogManager.Configuration.Variables["connectionString"] = Configuration["ConnectionString:SqlServer:Value"];
- LogManager.Configuration.Variables["configDir"] = Configuration.GetSection("LogFilesDir").Value;
- }
- }
2、方法二
步骤一:
- public class Startup
- {
- public Startup(IConfiguration configuration, IHostingEnvironment env)
- {
- Configuration = configuration;
-
- //配置NLog(1)
- env.ConfigureNLog("nlog.config");
- LogManager.Configuration.Variables["connectionString"] = Configuration["ConnectionString:SqlServer:Value"];
- LogManager.Configuration.Variables["configDir"] = Configuration.GetSection("LogFilesDir").Value;
- }
- }
步骤二:
- public class Startup
- {
- public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
- {
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- else
- {
- app.UseExceptionHandler("/Home/Error");
- }
-
- //配置NLog(2)
- loggerFactory.AddNLog();
- app.AddNLogWeb();
- }
- }
- public IActionResult Index()
- {
- ViewData["Err"] = string.Empty;
-
- try
- {
- LogEventInfo ei = new LogEventInfo(NLog.LogLevel.Info, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "获取PersonalAccount_Apply成功");
- ei.Properties["application"] = "myApplication";
- Logger logger = LogManager.GetCurrentClassLogger();
- logger.Log(ei);
-
- int a = 1 - 1;
- int result = 5 / a;
- }
- catch (Exception ex)
- {
- LogEventInfo ei = LogEventInfo.Create(NLog.LogLevel.Error, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, ex, null, "获取PersonalAccount_Apply失败");
- ei.Properties["application"] = "myApplication";
- ei.Properties["callsite"] = "callsite";
- Logger logger = LogManager.GetCurrentClassLogger();
- logger.Log(ei);
- string message = ex.Message;
- }
- return View();
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。