nlog.config 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. autoReload="true"
  5. throwExceptions="true"
  6. internalLogLevel="Info"
  7. internalLogFile="Logs/internal-nlog.txt">
  8. <extensions>
  9. <add assembly="NLog.Web.AspNetCore"/>
  10. </extensions>
  11. <!-- 定义变量当前应用程序名称 -->
  12. <variable name="AppName" value="Business" />
  13. <!-- 日志输出目标 -->
  14. <targets>
  15. <!--把日志输出到控制台 -->
  16. <target xsi:type="Console" name="lifetimeConsole" layout="${level:truncate=4:tolower=true}: ${logger}[0]${newline} ${message}${exception:format=tostring}" />
  17. <!-- 把日志记录到文件(通用) -->
  18. <target xsi:type="File" name="allfile" fileName="Logs/nlog-${shortdate}.log" encoding="UTF-8"
  19. archiveFileName="/Logs/nlog-${shortdate}.{#}.log" archiveAboveSize="10485760"
  20. layout="${longdate}|${event-properties:item=EventId_Id:whenEmpty=0}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
  21. <!--把日志输出到数据库 -->
  22. <target xsi:type="Database" name="database" dbProvider="MySqlConnector.MySqlConnection, MySqlConnector" connectionString="${configsetting:item=ConnectionStrings.DOPLog}">
  23. <install-command ignoreFailures="true">
  24. <text>
  25. <!-- NOTE: call LogManager.Configuration.Install(new InstallationContext()); to execute this query. -->
  26. CREATE TABLE IF NOT EXISTS `Sys_Log${date:format=yyyyMM}`(
  27. `id` bigint NOT NULL AUTO_INCREMENT,
  28. `createtime` datetime NOT NULL,
  29. `appname` varchar(50) NOT NULL,
  30. `modulename` varchar(50) NULL,
  31. `level` varchar(50) NOT NULL,
  32. `logger` varchar(1024) NULL DEFAULT NULL,
  33. `msg` text NULL,
  34. `userid` varchar(50) NULL DEFAULT NULL,
  35. `url` varchar(1024) NULL DEFAULT NULL,
  36. `ip` varchar(255) NULL DEFAULT NULL,
  37. `tenantId` varchar(50) NULL,
  38. PRIMARY KEY (`id`) USING BTREE
  39. );
  40. </text>
  41. </install-command>
  42. <commandText>
  43. INSERT INTO `Sys_Log${date:format=yyyyMM}`(`createtime`, `appname`, `modulename`, `level`, `logger`, `msg`,`userid`, `url`, `ip`,`tenantId`) VALUES (@CreateTime, @AppName,@ModuleName, @Level, @Logger, @Msg, @UserId, @Url, @IP,@TenantId);
  44. </commandText>
  45. <parameter name="@CreateTime" layout="${longdate}" />
  46. <parameter name="@AppName" layout="${var:AppName}" />
  47. <parameter name="@ModuleName" layout="${event-context:item=ModuleName}" allowDbNull="true"/>
  48. <parameter name="@Level" layout="${level}" />
  49. <parameter name="@Logger" layout="${logger}" allowDbNull="true" />
  50. <parameter name="@Msg" layout="${event-context:item=Msg}" allowDbNull="true" />
  51. <parameter name="@UserId" layout="${aspnet-user-claim:userId}" allowDbNull="true" />
  52. <parameter name="@Url" layout="${aspnet-request-url}" allowDbNull="true" />
  53. <parameter name="@IP" layout="${aspnet-request-ip}" allowDbNull="true" />
  54. <parameter name="@TenantId" layout="${event-context:item=TenantId}" allowDbNull="true" />
  55. </target>
  56. <!--<target xsi:type="Database" name="database" dbProvider="Microsoft.Data.SqlClient.SqlConnection, Microsoft.Data.SqlClient" connectionString="${configsetting:item=ConnectionStrings.DOPLog}">
  57. <install-command ignoreFailures="true">
  58. <text>
  59. --><!-- NOTE: call LogManager.Configuration.Install(new InstallationContext()); to execute this query. --><!--
  60. if not exists (select * from sysobjects where id = object_id('dbo.Sys_Log${date:format=yyyyMM}')
  61. and OBJECTPROPERTY(id, 'IsUserTable') = 1)
  62. create table dbo.Sys_Log${date:format=yyyyMM}
  63. (
  64. id bigint identity(1,1) not null,
  65. createtime datetime NOT NULL,
  66. appname varchar(50) NOT NULL,
  67. modulename varchar(50) NULL,
  68. level varchar(50) NOT NULL,
  69. logger varchar(1024) NULL DEFAULT NULL,
  70. msg text NULL,
  71. userid varchar(50) NULL DEFAULT NULL,
  72. url varchar(1024) NULL DEFAULT NULL,
  73. ip varchar(255) NULL DEFAULT NULL,
  74. tenantId varchar(50) NULL,
  75. CONSTRAINT [PK_dbo.Sys_Log${date:format=yyyyMM}] 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]
  76. )
  77. </text>
  78. </install-command>
  79. <commandText>
  80. INSERT INTO dbo.Sys_Log${date:format=yyyyMM}(createtime, appname, modulename, level, logger, msg,userid, url, ip,tenantId) VALUES (@CreateTime, @AppName,@ModuleName, @Level, @Logger, @Msg, @UserId, @Url, @IP,@TenantId);
  81. </commandText>
  82. <parameter name="@CreateTime" layout="${date}" />
  83. <parameter name="@AppName" layout="${var:AppName}" />
  84. <parameter name="@ModuleName" layout="${event-context:item=ModuleName}" allowDbNull="true"/>
  85. <parameter name="@Level" layout="${level}" />
  86. <parameter name="@Logger" layout="${logger}" allowDbNull="true" />
  87. <parameter name="@Msg" layout="${event-context:item=Msg}" allowDbNull="true" />
  88. <parameter name="@UserId" layout="${aspnet-user-claim:userId}" allowDbNull="true" />
  89. <parameter name="@Url" layout="${aspnet-request-url}" allowDbNull="true" />
  90. <parameter name="@IP" layout="${aspnet-request-ip}" allowDbNull="true" />
  91. <parameter name="@TenantId" layout="${event-context:item=TenantId}" allowDbNull="true" />
  92. </target>-->
  93. </targets>
  94. <!-- 日志输出规则 -->
  95. <rules>
  96. <!--All logs, including from Microsoft-->
  97. <!--<logger name="*" minlevel="Trace" writeTo="allfile" />-->
  98. <!--Output hosting lifetime messages to console target for faster startup detection -->
  99. <logger name="Microsoft.Hosting.Lifetime" minlevel="Debug" writeTo="allfile,database" final="true" />
  100. <logger name="Microsoft.EntityFrameworkCore.Model.Validation" maxlevel="Error" writeTo="allfile,database" final="true" />
  101. <!--Skip non-critical Microsoft logs and so log only own logs (BlackHole) -->
  102. <logger name="Microsoft.*" maxlevel="Info" final="true" />
  103. <logger name="System.Net.Http.*" maxlevel="Info" final="true" />
  104. <logger name="*" minlevel="Trace" writeTo="allfile,database" />
  105. </rules>
  106. </nlog>