nlog

How to get path of current target file using NLog in runtime?

人走茶凉 提交于 2019-12-02 16:58:15
I use NLog with next configuration: <targets> <target xsi:type="File" name="f" fileName="${basedir}/logs/${shortdate}.log" layout="${longdate} ${uppercase:${level}} ${message}" /> </targets> <rules> <logger name="*" minlevel="Trace" writeTo="f" /> </rules> I tried to get FileName property of FileTarget (I check, that there only one FileTarget in collection) NLog.LogManager.GetCurrentClassLogger().Info("test"); var logFile = (from t in NLog.LogManager.Configuration.AllTargets where t is NLog.Targets.FileTarget select (NLog.Targets.FileTarget)t).FirstOrDefault(); But logFile.FileName contains

Asp.Net Core中使用NLog记录日志

孤街浪徒 提交于 2019-12-02 15:56:32
2019/10/28, Asp.Net Core 3.0, NLog 4.6.7, NLog.Web.AspNetCore 4.9.0 摘要:NLog在asp.net网站中的使用,NLog日志写入数据库,NLog日志写入文件 需求 1.日志自动写入到数据库、写入到文件 2.appsettings.json数据库连接更改后,不需要去改NLog中的连接地址,启动网站或项目时自动检测变动然后去更改,以appsettings.json为准,保持同步。 3.写入日志时,除了NLog自带的字段,新增LogType自定义字段记录日志类型,例如网站日志、中间件日志等 4.统一的写日志方法,logger放在单例中,不用每次get一个logger对象来记日志 安装包 在nuget中安装NLog和NLog.Web.AspNetCore,这两个是NLog相关 还需要安装NLog写入数据库的数据库适配器,我这里写入到MySQL数据库,所以安装MySql.Data 如果是写入到SQL server数据库,需要安装Microsoft.Data.SqlClient NLog.config配置 网站根目录下新建 NLog.config 配置文件,记得右击该文件“属性”,复制到输出目录:“始终复制” NLog.config文件内容: <?xml version="1.0" encoding="utf-8"?>

line numbers are disappear on production in the NLog logs

二次信任 提交于 2019-12-02 15:23:12
问题 I configured NLog layout for having method name and line number by using ${callsite} parameters and it works well locally as below. Application_Start(Global.asax.cs:33) But it is changing to Application_Start without line numbers on production. I guess that it is because of it can not access line numbers without .pdb files but i am using .NET 4.5 that makes possible to access line numbers via caller attributes without .pdb files. And i understand NLog haven't optimized itself for .NET 4.5. Is

How to control Nlog logLevel using environment variables

好久不见. 提交于 2019-12-02 15:21:35
问题 Trying to override LogLevel in Nlog Configuration using environment variable and it does not work: e.g <logger name="*" writeTo="console"> <filters> <when condition="level >= '${environment:LOG_LEVEL}' " action="Ignore"/> </filters> </logger> where LOG_LEVEL is set to LogLevel.Info Same config with Nlog Env var works: <variable name="myvar1" value="LogLevel.Info"/> <logger name="*" writeTo="console"> <filters> <when condition="level >= '${myvar1}' " action="Ignore"/> </filters> </logger> Any

Getting Logger Name into Excel file with NLog

房东的猫 提交于 2019-12-02 15:20:57
问题 Thanks to Rolf's comment in this question: NLog in C# with severity AND categories I am able to record to a text file the category of log message (such as "Thermal" or "Database" or "Mechanical". I'm doing this simply by passing a name to the "GetLogger" method. public MainWindow() { InitializeComponent(); var logger = NLog.LogManager.GetCurrentClassLogger(); logger.Info("Hello World"); (NLog.LogManager.GetLogger("Database")).Info("Here is a DB message"); (NLog.LogManager.GetLogger("Thermal")

How to control Nlog logLevel using environment variables

妖精的绣舞 提交于 2019-12-02 08:25:07
Trying to override LogLevel in Nlog Configuration using environment variable and it does not work: e.g <logger name="*" writeTo="console"> <filters> <when condition="level >= '${environment:LOG_LEVEL}' " action="Ignore"/> </filters> </logger> where LOG_LEVEL is set to LogLevel.Info Same config with Nlog Env var works: <variable name="myvar1" value="LogLevel.Info"/> <logger name="*" writeTo="console"> <filters> <when condition="level >= '${myvar1}' " action="Ignore"/> </filters> </logger> Any hints how to make use of env variables? Some extension to write? I know this is a little more wordy,

Can NLog preserve callsite information through c# extension methods?

那年仲夏 提交于 2019-12-02 08:22:35
EDIT: While similar, this is not the same as the questions about using an NLog wrapper. Extension methods add another level of indirection which makes even a proper wrapper report the wrong callsite I currently use a logging wrapper around NLog, and I use the trick they show in the example in their source for getting accurate callsite info. For a new project I started, I wanted to create a simpler class, so I just implemented something like the following interface: public interface ILogger { void Log( LogEntry entry ); } And then I created an extension method class like: public static class

Getting Logger Name into Excel file with NLog

你离开我真会死。 提交于 2019-12-02 03:55:11
Thanks to Rolf's comment in this question: NLog in C# with severity AND categories I am able to record to a text file the category of log message (such as "Thermal" or "Database" or "Mechanical". I'm doing this simply by passing a name to the "GetLogger" method. public MainWindow() { InitializeComponent(); var logger = NLog.LogManager.GetCurrentClassLogger(); logger.Info("Hello World"); (NLog.LogManager.GetLogger("Database")).Info("Here is a DB message"); (NLog.LogManager.GetLogger("Thermal")).Info("Here is a Thermal message"); } The text file looks like this: 2018-05-13 17:40:47.7442|INFO

05 .NET CORE 2.2 使用OCELOT -- NLog

随声附和 提交于 2019-12-02 02:44:56
加入NLog 按照官网的文档 https://github.com/NLog/NLog/wiki/Getting-started-with-ASP.NET-Core-2 一步一步操作下来,即可设置好。 配置文件的设置参考 http://www.makaidong.com/%E5%8D%9A%E5%AE%A2%E5%9B%AD%E5%8D%9A%E6%96%87/20150927/110956.html 来源: https://www.cnblogs.com/zhanglinfeng715/p/11726673.html

Creating a database programatically in nlog to enable using DatabaseTarget

Deadly 提交于 2019-12-01 22:26:15
I'm creating a DatabaseTarget object in C# and using it to log data into an NLog database. If the database does not exist, the nlog target fails. I want to check to see if the DB exists, and if it doesn't create it and a log table. I can see the targetDB.Install(installationContext) function appears to be able to do the job but cannot find any examples. There are numerous examples using a config file. I want to put this in code and not have to deploy a config file in all applications that use the DLL that will contain this code. How do I check for and create the database? This code uses the