nlog

NLog, Elmah + shared library

只愿长相守 提交于 2019-12-06 04:18:57
问题 I am trying to come up with a way to create a common library for all my MVC projects. I started off really simple with BaseController and BaseModel classes. Easy stuff! Now in my projects I would like to either use Elmah or NLog for logging exceptions and/or tracing info. Can anyone give me some ideas on the best practices for writing a common library to support both? 回答1: I would use NLog as base for your logging. I've created a small target for NLog which could be used to route exceptions

Prevent expensive log call if log level is below threshold

╄→尐↘猪︶ㄣ 提交于 2019-12-06 04:11:31
If I do a NLog.Trace(): logger.Trace("Json: {0}", Newtonsoft.Json.JsonConvert.DeserializeObject(myObject)); And my minlevel is on Error: <logger name="*" minlevel="Error" writeTo="mail" enabled="false" /> Will my object be deserialized for nothing? Yes, of course yes. But how can I avoid this? if(logger.IsTraceEnabled) logger.Trace("Json: {0}", Newtonsoft.Json.JsonConvert.SerializeObject(myObject)); See IsTraceEnabled . This is good practice for logging calls where the call itself can be expensive (like in your case above) or in a repetitive high call loop. For everything else there usually is

How to make a certain piece optional in a NLog layout pattern?

此生再无相见时 提交于 2019-12-06 03:40:17
问题 I have a custom layout renderer named job . It provides several items, which are used like this in our app.config: <variable name="jobHost" value = "${job:item=host}" /> <variable name="jobService" value = "${job:item=service}" /> <variable name="jobNS" value = "${job:item=ns}" /> <variable name="jobName" value = "${job:item=name}" /> <variable name="jobKind" value = "${job:item=kind}" /> <variable name="jobScheduleId" value = "${job:item=scheduleId}" /> <variable name="jobLayout" value = "[H

Check to see if an log event occurs in NLog

元气小坏坏 提交于 2019-12-06 00:25:16
I am trying to check to see if a log event happens in my app, and if it does something. I have checked everywhere and can't seem to find any info on if an log event even happens. private static Logger logger = LogManager.GetCurrentClassLogger(); logger.Info("Info Log Message"); if(logger.event == true) { //an log even happen this run } It looks like you can use the MethodCall target to accomplish this. You can add a target entry to your NLog.config file that would direct log events to a static method in one of your classes. See the documentation for more details on how to do that. You can also

using Nlog and writing to file as json

烂漫一生 提交于 2019-12-06 00:07:54
问题 I think I'm missing something as I can't seem to figure out how to have it write to a log file in json format using NLog setup in configuration file. The straight rolling file works fine, but not the json. The json target only outputs the message (not in json). <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <targets async="true"> <target xsi:type="File" name="rollingFile" fileName="${basedir}/logs/${shortdate}.log"

Using Stored Procedure in NLog on a Database target

对着背影说爱祢 提交于 2019-12-05 22:08:55
问题 I am having a bit of a problem using a stored procedure instead of a SQL INSERT statement when using NLog in a C# web application. The connection string "Logger" is correctly configured in Web.config and works properly when replacing the commandText with a SQL statement. I would appreciate a hint in the right direction. In this example the stored procedure is under the "Logs" schema and it is called "LogError". <targets> <target xsi:type="Database" name="dberrorlog" connectionStringName=

Getting NLog to send out JSON with proper headers?

这一生的挚爱 提交于 2019-12-05 20:42:26
问题 Trying to use NLog to send JSON-encoded logs out of our C# app, to dump them into CouchDB. However, using the Network target, I can't seem to find a way to set the Content-Type header properly; and CouchDB won't have anything to do with my input otherwise. CouchDB is out-of-the-box, Current NLog config: <nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true"> <variable name="FileLayout" value='{ "date":"${longdate

How to Log to Elastic Search by NLog or SeriLog with authentications

馋奶兔 提交于 2019-12-05 18:47:58
We are currently use azure scale set (many VMs on one source group with load balance and one availability set), we used to use NLog to log our web app action and errors, but now we asked/needs to use Elastic Search and also use centralized log for all azure vm instances instead of file per each instance. I am new to ES and LogStash concepts, Do I need to replace NLog with something else? and How I move to use ES and unify all logs in one (I think to make nlog store in azure storage table as unify results or do I needs to use LogStash or you prefer something else)? What is the most logging that

Setting up C# solution with multiple projects using NLog in Visual Studio

冷暖自知 提交于 2019-12-05 16:36:59
问题 My solution in Visual Studio 2012 currently contains two projects: DLL WPF application (which requires methods of the DLL) Both, the DLL and the WPF application, use NLog for logging. Currently each project contains the NLog DLL itself. Here is what I don't understand: It seems unnecessary to me including the identical NLog DLL in each project. The DLL however shall be reusable in other solutions, i.e. somehow the NLog DLL must be contained in the DLL project. What would be an adequate way of

NLog dynamically change filename using NLog.config

随声附和 提交于 2019-12-05 15:16:58
How to dynamically change the FileName using a variable from C#? My idea is to create a log file like Log_<UserId_From_DB>_${date:format=yyyy-MM-dd}.log . Any ideas? Julian Another option is to use the Global Diagnostic Context - $(GDC) : Set the value in C# GlobalDiagnosticsContext.Set("UserId_From_DB","42"); In the config (nlog.config): <target type="file" filename="Log_${gdc:item=UserId_From_DB}_${date:format=yyyy-MM-dd}.log" ..> Please avoid modifying NLog Variables at runtime (See previous answer below). They should be seen as readonly, because they are not threadsafe. NLog Variables will