I'm trying to send the output to the console (or colouredconsole) ... which I'm hoping would (also?) go to the Visual Studio's Output window for any ASP.NET web site/app/mvc app.
It doesn't by default, but if I change the target to 'file' then it works for sure.
Can NLog output to the Output window for web apps?
You can use this configuration file (nlog.config in the app path):
<?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">
<targets>
<target name="debugger" xsi:type="Debugger" layout="${logger}::${message}"/>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="debugger" />
</rules>
</nlog>
See also: https://github.com/NLog/NLog/wiki/Debugger-target
-Scott
Adding to Scott P's answer, you can add a filter for when the environment is not "Development" to prevent any slowdowns in Staging/Production etc.
<logger name="*" minlevel="Trace" writeTo="debugger">
<filters defaultAction="Ignore">
<when condition="'${environment:ASPNETCORE_ENVIRONMENT}' == 'Development'" action="Log" />
</filters>
</logger>
Related
I am running an aspnet core consol program on centos.I have set up nlog.config to Generate log files. All is well in my windows development environment,but when I run it on Centos I can't get logs files using my settings.
My setting:
<target xsi:type="File" name="target1" fileName="${currentdir}\logs\${shortdate}-nlog.log" ... />
In centos:(click to show img)
It will create a file name "demoBS\logs\2019-06-26-nlog.log" in parent directory.
Thank you and look forward to your help!
Ps: Nlog.config
<?xml version="1.0" encoding="utf-8" ?>
<!-- XSD manual extracted from package NLog.Schema: https://www.nuget.org/packages/NLog.Schema-->
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true"
internalLogFile="${currentdir}\logs\${shortdate}-internal.log"
internalLogLevel="Info" >
<!-- the targets to write to -->
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="target1" fileName="${currentdir}\logs\${shortdate}-nlog.log"
layout="${date}|${level:uppercase=true}|${message} ${exception}|${logger}|${all-event-properties}" />
<target xsi:type="Console" name="target2"
layout="${date}|${level:uppercase=true}|${message} ${exception}|${logger}|${all-event-properties}" />
</targets>
<!-- rules to map from logger name to target -->
<rules>
<logger name="*" minlevel="Trace" writeTo="target2,target1" />
</rules>
</nlog>
Make sure to use Unix-path instead of Windows-path. Ex:
${currentdir}/logs/${shortdate}-nlog.log
Turn the slashes the other way
I have multiple ASP .NET MVC and Web Api apps hosted on Azure.
I use two differents stages for deployment : stage and PROD. Basically, I want to have two different folders for logs : logs-stage and logs-PROD. This is working well based on my web.config and properties set directly in Azure.
The issue is that each time I deploy, all the previous logs are deleted. How can I avoid that ?
My NLog config looks like the following :
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target xsi:type="File" name="f" fileName="${basedir}/logs-${appsetting:name=Version:default=DEV}/website.${shortdate}.log"
layout="${longdate} ${uppercase:${level}} ${logger} - ${message}" />
<target name="email-Errors" xsi:type="Mail"
smtpServer="smtp.sendgrid.net"
smtpPort="myPort"
enableSsl="false"
smtpUsername="myUsername"
smtpPassword="myPassword"
smtpAuthentication="Basic"
from="myEmail"
to="${appsetting:name=WEBSITE_EMAIL_DEVELOPERS:default=myEmail}"
subject="[${appsetting:name=Version:default=DEV}][WEB][${uppercase:${level}}]"
layout="${longdate} ${uppercase:${level}} ${logger} - ${message}"
html="false" />
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="f" />
<logger name="*" minlevel="Error" writeTo="email-Errors" />
</rules>
</nlog>
The issue is that each time I deploy, all the previous logs are deleted. How can I avoid that ?
According to your description, it seems that when you publish the WebApp then remove additional file in the Azure WebApp. If it is that case please have a try to uncheck the [Remove additional files as destination] option during publish the WebApp.
Updated:
From the blog, we could know that if we swap the slots, just the DNS pointer changes, so if you have the nlog in the product after swap then the Nlog file is in the staging slot that not deleted. The following the snippet from the blog.
In short, the Swap operation will exchange the website's content between 2 deployment slots.
Swapped and what is not but note that swap is not about copying the content of the website but more about swapping DNS pointers.
I am new to using NLog with ASP.NET Core, so I have followed the guide here:
https://github.com/NLog/NLog.Web/wiki/Getting-started-with-ASP.NET-Core-(project.json)
I have created the following nlog.config file at the root of the project directory:
<?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">
<extensions>
<add assembly="NLog.Web.AspNetCore"/>
</extensions>
<!-- define various log targets -->
<targets>
<!-- write logs to file -->
<target xsi:type="File" name="allfile" fileName="${basedir}\nlog-all-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}|${message} ${exception}" />
<target xsi:type="File" name="ownFile-web" fileName="${basedir}\nlog-own-${shortdate}.log"
layout="${longdate}|${event-properties:item=EventId.Id}|${logger}|${uppercase:${level}}| ${message} ${exception}|url: ${aspnet-request-url}|action: ${aspnet-mvc-action}" />
<target xsi:type="Null" name="blackhole" />
</targets>
<rules>
<!--All logs, including from Microsoft-->
<logger name="*" minlevel="Trace" writeTo="allfile" />
<!--Skip Microsoft logs and so log only own logs-->
<logger name="Microsoft.*" minlevel="Trace" writeTo="blackhole" final="true" />
<logger name="*" minlevel="Trace" writeTo="ownFile-web" />
</rules>
</nlog>
Inside a controller, I call a line like this one:
_logger.LogInformation("Entered CustomerRange method");
which returns the following in the output window in Visual Studio:
CustomerMgmtAPI.Controllers.CustomerController:Information: Entered CustomerRange method
However, the actual log files are never created by NLog. I was wondering if someone can point out the error in the NLog configuration here, since I have been reviewing the documentation of NLog for ASP.NET Core project and I can't find the error myself.
So the actual fix to the problem was the remove the first line from the nlog.config file:
<?xml version="1.0" encoding="utf-8" ?>
I remove this line and everything started working as expected. I also noticed that Visual Studio was giving me these errors when that line was present:
Invalid token 'Text' at root level of document.
Unexpected XML declaration. The XML declaration must be the first node in the document and no white space characters are allowed to appear before it.
It seems in this case that the NLog tutorial is broken, as I just took over this file from the sample for ASP.NET Core. I am using VS2017, so perhaps there is an incompatibility with this version of VS?
The dirty little secret about using NLog with ASP.NET Core is that you can configure and create logs just as you did in ASP.NET and ASP.NET MVC. You just use the regular NLog Nuget package like you normally would.
Just create an NLog.config in your root, etc. You don't even have to make any extra configurations in the config or elsewhere to get it to work. You just reference NLog in your class and then create a logger with the LogManager.
What this means is that you don't have all of the wireup in Program.cs etc.
I am trying to integrate NLog in AspNet 5 (or using the new name AspNet Core 1.0) web app. Not sure if it is possible at all but I want to log the currently logged in user.
This is my NLog config file.
<?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">
<targets>
<target name="logfile"
xsi:type="File"
fileName="file.txt"
layout="${longdate}|${message}|${identity}|${aspnet-user-identity}" />
</targets>
<rules>
<logger name="WebApplication2.*" minlevel="Info" writeTo="logfile" />
</rules>
</nlog>
The thing is that I get
System.ArgumentException: LayoutRenderer cannot be found: 'aspnet-user-identity'
My project.json file looks like:
"dependencies": {
.....
"NLog.Framework.logging": "1.0.0-rc1-final",
"NLog": "4.4.0-alpha1",
"NLog.Config": "4.3.0-beta1",
"NLog.Extended": "4.0.0-rc",
"NLog.Web": "4.1.0"
},
Maybe I am missing something, or it is not yet supported by NLog ?
EDIT: I found out that auto load of extensions is not currently supported. So, I have to modify my NLog.config like:
<extensions>
<add assembly="NLog.Web"/>
<add assembly="NLog.Extended"/>
</extensions>
But now I get blank input for the current user. Any ideas ?
The internal log will give some more info why loading the extension failed.
There are also other ways to load the extensions, the assemblyFile attribute and programmatically:
//target
ConfigurationItemFactory.Default.Targets
.RegisterDefinition("MyFirst", typeof(MyNamespace.MyFirstTarget));
//layout renderer
ConfigurationItemFactory.Default.LayoutRenderers
.RegisterDefinition("hello-world", typeof(MyNamespace.HelloWorldLayoutRenderer ));
Edit: did some tests. This works in loading the assembly:
<extensions>
<add assembly="NLog.Web" />
</extensions>
But too bad NLog.Web isn't ASP.NET 5 compatible, yet. We can't use HttpContext.Current there.
edit: an ASP.NET 5 compatible version of NLog.Web is now available! See NuGet
I used the following code in c# to get policies\rules from deployed application in BizTalk server.
BTSTask.exe ListApp -ApplicationName:"EAISolution" -ResourceSpec:"c:\EAISolution.PolicyInf
o.xml" /Server:VHYDTRBELSUP-02 /Database:BizTalkMgmtDb
From above command I got the output as below
<?xml version="1.0" encoding="utf-16" ?>
<ResourceSpec xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ApplicationName="EAISolution" xmlns="http://schemas.microsoft.com/BizTalk/ApplicationDeployment/ResourceSpec/2004/12">
<Resources>
<Resource Type="System.BizTalk:BizTalkAssembly" Luid="EAIOrchestration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=97e0f507fd7fd10d" />
<Resource Type="System.BizTalk:BizTalkAssembly" Luid="EAIServices, Version=1.0.0.0, Culture=neutral, PublicKeyToken=97e0f507fd7fd10d" />
<Resource Type="System.BizTalk:BizTalkAssembly" Luid="FFSchemasTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=97e0f507fd7fd10d" />
<Resource Type="System.BizTalk:Rules" Luid="RULE/ProcessPurchaseOrder/1.0" />
<Resource Type="System.BizTalk:BizTalkBinding" Luid="Application/EAISolution" />
</Resources>
</ResourceSpec>
and from BizTalk server I got the below output using policy export in BizTalk server administration
<?xml version="1.0" encoding="utf-8" ?>
<brl xmlns="http://schemas.microsoft.com/businessruleslanguage/2002">
<ruleset name="ProcessPurchaseOrder">
<version major="1" minor="0" description="" modifiedby="username" date="2013-05- 27T12:04:55.6121122+05:30" />
<configuration />
<bindings>
<xmldocument ref="xml_31" doctype="RuleTest.PO" instances="16" selectivity="1" instance="0">
<selector>/*[local-name()='PurchaseOrder' and namespace-uri() ='http://EAISolution.PurchaseOrder']/*[local-name()='Item' and namespace-uri()='']</selector>
<selectoralias>/PurchaseOrder/Item</selectoralias>
<schema>....\PO.xsd</schema>
</xmldocument>
<xmldocument ref="xml_32" doctype="RuleTest.PO" instances="16" selectivity="1" instance="0">
<selector>/*[local-name()='PurchaseOrder' and namespace-uri()='http://EAISolution.PurchaseOrder']
</selector>
<selectoralias>/PurchaseOrder</selectoralias>
<schema>....\PO.xsd</schema>
</xmldocument>
</bindings>
<rule name="ApprovalRule" priority="0" active="true">
<if>
<compare operator="less than or equal to">
<vocabularylink uri="3f0e9bcc-6212-4e6a-853c-e517f157a626" element="d4eb2deb-06d3-42c4-af49-ceb21331b1cc" />
<lhs>
<function>
<xmldocumentmember xmldocumentref="xml_31" type="int" sideeffects="false">
<field>*[local-name()='Quantity' and namespace-uri()='']</field>
<fieldalias>Quantity</fieldalias>
</xmldocumentmember>
</function>
</lhs>
<rhs>
<constant>
<int>500</int>
</constant>
</rhs>
</compare>
</if>
<then>
<function>
<xmldocumentmember xmldocumentref="xml_32" type="string" sideeffects="true">
<field>*[local-name()='Status' and namespace-uri()='']</field>
<fieldalias>Status</fieldalias>
<argument>
<constant>
<string>Approved</string>
</constant>
</argument>
</xmldocumentmember>
</function>
</then>
</rule>
</ruleset>
</brl>
So please let me know how to get the output of second using command line.
BTSTask will only export the policy as part of an MSI (see below).
You could then extract the MSI (see How to extract msu/msp/msi fileds from the command line) to get the policy file.
From How to Import a Policy
BTSTask does not provide a specific command for importing (or exporting) policies; however you can use the ExportApp command of BTSTask to selectively export only the policies in an application that you want, including no other application artifacts. Then you can use the ImportApp command to import the .msi file into an application in a different BizTalk group. This is the approach described in this topic. When you do this, the policy is automatically imported and published in the BizTalk group and added to the specified application.
The below steps will get export the policy, but as part of an MSI.
From How to Export a Policy
Use the BTSTask ListApp command with the /ResourceSpec option to generate an XML file that lists the artifacts in the BizTalk application from which you want to export a policy, as described in ListApp Command.
Edit the XML file generated in the previous step, deleting all of the artifacts except for the policy or policies that you want to export.
Use the BTSTask ExportApp command, and specify the modified XML file for the /ResourceSpec parameter. For more information, see ExportApp Command.
BTSTask exports the specified policies and all of their associated vocabularies into an application .msi file.