NLog - I want to use NLog.config rather than App.config - nlog

On oppose to documentation, C# Application does not read NLog.config
(But In App.config, 'nlog' section is well read.)
So, Target and Rule is not working.

Related

Is it possible to add as link NLog.config file?

I use NLog 4.4.12, .NET 4.5.2
I am trying to add NLog.config file as link, but then no logs are logged.
Is it possible to add as link NLog.config file?
Sure, but it's important to enable "copy to output directory"

NLog and the include file option

Ok, this has been doing my head in all day. In my UAT setup I have the following in my web.config
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance>
<include file="d:\logs\nlog.config"/>
</nlog>
This pulls in all my rules and targets and has been working fine. That was until I published to our prod server. Same setup but it fails to find the nlog.config file.
At first I thought it was NTFS permissions, but Nlog can write to the same folder location as where this config file resides. I used the internal logging to get an idea what was happening. Nlogs internal log just says that the file doesn't exist. I can't seem to identify why the same setup works in one system but not on another. I've added all relevant NTFS permissions (triple checked) and even went as far as granting 'everyone' read rights as a test, still no joy.
I'm guessing there's something going on within IIS that is stopping the app from reading outside of the root web folder on prod? Which would be odd given it can write to the same place.
It's ok, I was being a donut, well sort of. I created a text file and named it nlog.config not realising that .txt had been stuck on the end. Now the problem makes sense! It never clicked because the .txt was hidden and showing the name as nlog.config in explorer. :o)

What is the name of the web role .config file with SDK 2.2

I have a web role in which I have extended the RoleEntryPoint to do some work that is outside of the scope of the web site. As part of the RoleEntryPoint.Run() my code is required to read from the .config using ConfigurationManager.
While this is a little unusual, using SDK 1.8 I was able to make this work by ensuring that my package included a [The name of my project].dll.config file.
Now that I have upgraded to SDK 2.2 when I try to use .AppSettings or .GetSection() the values are always null, which leads me to believe it is unable to find my file.
I have tried deploying a Worker Role and the .config file still follows the same name pattern that I'm currently using.
I have also tried naming the file WaIISHost.exe.config.
I am aware that ideally this configuration should be included in the .csfg file, but my questions is does anyone know what I should be calling my config file?
UPDATE:
With the help of this question, I now know that the name of the config file it is reading from is E:\base\x64\WaIISHost.exe.Config, but I don't know why this has changed or what I can to overide this.
After much investigation and trial an error I finally have a solution.
The name of the file is still required to be [The name of my project].dll.config, but you need to make sure that this file is in your approot\bin\ directory of your package.
I believe my initial problem was caused by the Copy to Output Directory property being changed to Do Not Copy, although I'm unsure how this happened. If you find yourself in a similar situation you can just add a file with the correct name to your project and set the Copy to Output Directory to be Copy Always.
Once I'd done that however I realised I had another problem. I needed the .config file to have had the config transformations run over it, which this didn't do. To fix this I updated the .ccproj file to have the following:
<PropertyGroup>
<!-- The first two of these targets are what is defined in the base SDK 2.2 targets file. When we upgrade we may need to look reassess this. -->
<CopyRoleFilesDependsOn>
CopyWebRoleFiles;
CopyWorkerRoleFiles;
CopyWebConfigToBin;
</CopyRoleFilesDependsOn>
</PropertyGroup>
<Target Name="CopyWebConfigToBin">
<!-- We need to copy the transformed Web.config to the bin directory so that the RoleEntryPoint has access to the config settings.-->
<Message Text="Copy %(WebRoleReferences.OutputDir)Web.config tp %(WebRoleReferences.OutputDir)\bin\BackOffice.UI.dll.config" Importance="high" />
<Copy SourceFiles="%(WebRoleReferences.OutputDir)Web.config" DestinationFiles="%(WebRoleReferences.OutputDir)bin\[Name of project].dll.config" />
</Target>
This adds an extra target which waits until all of the other files have been copied to the appropriate directory and then picks up the web.config and puts a copy in the bin directory with the correct name.
Are you able to put the config values into the Azure config file (the .cscfg) rather than using the .config file? You can read the values from the cscfg via the RoleEnvironment.GetConfigurationSettingValue static method.
This page explained why it's called WaIISHost.exe.Config and where you can put it in your project.
http://azure.microsoft.com/blog/2010/12/02/new-full-iis-capabilities-differences-from-hosted-web-core/
Like knightpfhor mentioned, you can also use [AssemblyName].dll.config to put these configuration too. It depends on the assembly name of your project, you can check property of your web role project.

How do I override default nlog configuration?

We ship a framework assembly for logging which internally uses nlog. We also ship an embedded nlog configuration as a resource in our assembly and read this on startup (a static constructor within our wrapper which wraps over the nlog wrapper using the XmlLoggingConfiguration class).
We would now like developers to be able to specify/override additional configuration via an external file for their own loggers (using the same nlog configuration file format).
Can we:
Refer include files in our embedded nlog configuration file? How is the path of the include file resolved? If we do this, does the content of the include file override the content of the embedded nlog configuration we read on startup?
Can developers use the nlog configuration section? Does the content of this section override the default configuration we have specified pro grammatically?
Any other options?
How is the path of the include file resolved?
You can use in the included file name:
layout renderers such as ${baseDir},
nlog variables.
If the resulting filename is not absolute, it is combined with the directory, where the original NLog configuration was located, but this may be null in your case, so you are limited to first 2 options.
does the content of the include file override the content of the embedded nlog configuration?
No, this content is added to the content of the embedded configuration.
Does the content of nlog configuration section override the default configuration we have specified programmatically?
When you set the configuration programatically, you override any configuration that was set before and also disable the automatic configuration discovery, including one from application configuration file or NLog.config.
But you can check whether the configuration is not null first time before you set the embedded value. This will trigger the automatic discovery, and in case if any logging configuration is found, the returned value would be non-null.

Logging via FileAppender

here is an easy question coming:
i am trying to use log4net to log the infos on a file. i wrote
< file value="log-file.txt" /> into my appender tag in app.config. and now wondering where the log-file.txt is positioned and whether it is created automatically or i should create it by myself.
i am using c# - wpf
It should be in the Debug\Bin or Release\Bin folder.
if its not there, try specifying full path.
The file will automatically be created if it doesn't exist.
It may require that the application has write permission to the folder where the logfile is placed.
As Orentet mentions this is normally the bin folder.

Resources