NLog and the include file option - iis

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)

Related

Where do logs go when running ASP.NET Core on IIS 7.5?

I'm posting this and answering it, because this left me stumped for a very long time. I have the following line in my web.config:
<aspNetCore processPath="dotnet" arguments=".\XXX.Server.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" />
So apparently, this should be logging to logs\stdout, but when I look, there's nothing there. I went on a wild goose chase, searching the whole disk for anything named 'log' (that returned too much) or 'stdout' (that returned nothing), and still couldn't figure it out.
You could probably check the Event Viewer on your machine -> Application to see if there are any errors logged, which could explain why your log files aren't being generated.
However, the most probable reason is that IIS doesn't have a permission to write to that log folder.
Right click on the folder -> Security
Ensure that IIS_IUSRS user has the following permissions: read and execute, list, write (potentially write is missing by default)
You must make sure the log folder exists! IIS won't make it for you. Such a simple solution to this infuriating problem.
Update - Nov 2022
The Microsoft documentation on Setting up logging for Asp.Net Core on IIS now specifically lists creating the 'Logs' folder as one of the steps. This implies that IIS will not create the folder if it doesn't exist.
This issue was raised in the Asp Net Core Module's repo on GitHub: Issue #30 - Logs are not created if the log folder does not exist. Their suggested workaround is to include a dummy '.log' file in the log directory, so that the log folder is created when you publish the site.
I created the logs folder but still, nothing was logged. I found out that you can use an existing folder under home/logfiles and make it work without creating a folder. This solution worked for me as is:
1) Open the web.config file that is created in the root folder of the published application.
2) Set stdoutlogEnabled to true and stdoutLogFile to \?\%home%\LogFiles\stdout like this:
<aspNetCore processPath="dotnet" arguments=".\SellCarsHereV2.dll" stdoutLogEnabled="true" stdoutLogFile="\\?\%home%\LogFiles\stdout" />
Then you can either go to that path and download the files or use the Azure Portal (if it's hosted in Azure). If you are using the Azure Portal:
1) Go to the app service.
2) Go to Advanced Tools which takes you to https://{your app service}.scm.azurewebsites.net/
3) Click on Debug Console menu --> CMD
4) Click on LogFiles
5) You'll see a file called stdout_*.log and you can click on the pencil to view it. You can also download the file if you want.
According to the provided stdoutLogFile=".\logs\stdout" in your web.config file, the logs directory should be created next to the web.config, inside the publish directory.
To create the logs subdirectory you can use the approach described in the ASP.NET Core directory structure.
Just paste this at the end of your published project's *.csproj file
<Project>
...
<Target Name="CreateLogsFolder" AfterTargets="Publish">
<MakeDir Directories="$(PublishDir)logs"
Condition="!Exists('$(PublishDir)logs')" />
</Target>
</Project>
During the publish process after this change the logs directory should be created if it does not exist in the output directory.
If you run for example:
dotnet publish --output <path_to_your_publish_folder> you should find the logs directory inside the <path_to_your_publish_folder>
I did the following steps in order to get logs:
Create a folder logs and give write access to IIS_IUSRS as described above
Set absolute path in web.config: stdoutLogFile="C:\xxx\xxx\logs"
Recycle the the application pool
Stop, Start the Website
Edit
Step 2 works with relative path too stdoutLogFile=".\logs\stdout"
Check the event viewer. If you're like me you will see an entry in the Application\Event log "Could not create stdoutLogFile c:....logs\stdout_....". You have to create the logs folder there yourself. Once I created the "logs" folder stdout_.... files began to dump into it. Of course also be sure stdoutLogEnabled="true". The location of the expected folder location will be shown in the event viewer log. This is important because it may not be where you think it should be located.
I noticed that the logs are not created immediately when you hit the website. I allowed write access for IIS_IUSRS, created 'logs' folder, restarted app_pool and after few minutes logs were there.

Include referenced assembly's configuration in cspkg

I have an executable that I want to be deployed together with my Azure web role. The executable has a configuration file that needs to be included as well.
I tried adding a reference to the executable's project in my web role project, which made the exe file appear in the bin folder of the cspkg, but not the configuration file.
How can I get the configuration file to be included as well?
It seems wrong to include it directly as a content file in the web role project because this file is a build artifact (app.config gets renamed to .config.exe during build).
Thanks!
In an early SDK they added the concept of Role Content folders, or folders you could point to in the service definition file and say anything in this folder, add it to the package and deploy it with the role. If you look at the schema for the Service Definition you'll see these listed on the both the web and worker roles schemas. You can manually add this and point to any location on the local system and anything in that directory will be picked up and included.
<WebRole name="SimpleWeb" vmsize="Small">
...
<Contents>
<Content destination="ConsoleApp">
<SourceDirectory path="c:\src\SimpleWebContent\ConsoleApp\BuildOutput" />
</Content>
</Contents>
</WebRole>
For example, you could point to the output directory of the build for your executable so that anything that is generated by your build for that executable would be included. You can set the destination directory in relation to the app root, but the tricky part is the source directory. Note in my example above the full path is provided. The documentation says that you can use a relative path, but I tried many combinations and the behavior seemed very quirky. The complete path does work.
The VS SDK tools didn't expose this until SDK 1.7 and it's still not very good. Phil Hoff did a blog post on it called "Add Files to your Windows Azure Package using Role Content Folders". Note that when you use this method of adding the files you won't see the content elements appear in your service definition. They get auto injected at package time. If you are doing this as part of a build process that may not happen since VS tooling is doing the injection, but to be fair I didn't try calling cspack directly to see if having the content elements included in the service definition file actually packaged those or not. Also, I found that just adding a new folder and just having files under that folder didn't seem to work. I had to actually add the files by name there, which seemed wrong. I did hack the .ccproj file to use a wildcard on the folder include, which did work, but also seemed like a hack to me.

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.

VS2012 pubxml and TransformWebConfigEnabled

Maybe someone can advise me.
I am trying to publish a project using VS2012. I have setup a publish profile to publish it to a file system drive.
I have several environments/build configurations required and thusly am using Web.Config transformations.
At the moment, I have a web.config + 5 transforms (one for each build config). When the solution is published, it successfully deploys to the file system specified. However it also transforms the Web.Config into web.config.
I have modified the .pubxml to include the <TransformWebConfigEnabled>False</TransformWebConfigEnabled> element, but the publish process just seems to ignore it and transform the web.config regardless.
Followed via this link: http://msdn.microsoft.com/en-us/library/dd465342(v=vs.100).aspx
Can anyone advise?
[edit]
I have tried putting <TransformWebConfigEnabled>False</TransformWebConfigEnabled> element into both the pubxml and the proj file and both ways it get's completely ignored and the web.config is transformed regardless.
You are sticking it in the wrong file. You want to modify
YourProject.csproj
instead of PublishProfile.pubxml.

How do I change the location of CruiseControl log files?

The server where CruiseControl.NET is already installed is running out of disk space.
Is there a simple way of moving the CruiseControl logs to another drive?
Found it here.. http://confluence.public.thoughtworks.org/display/CCNET/Xml+Log+Publisher
The Xml Log Publisher is used to create the log files used by the CruiseControl.NET Web Dashboard, so if you don't define an section the Dashboard will not function correctly.
You should place the in the section, after any File Merge Tasks, in your Project Configuration Block.
Examples
Minimalist example:
<xmllogger />
Full example:
<xmllogger logDir="c:\myproject\buildlogs" />
Regardless of moving logs somewhere else, you should also use Artifact Cleanup Publisher to delete older logs. It is done on a per-project base, usually set up to keep X last build logs or logs from X last days. Also, storing project's workingDirectory and artifactDirectory on a separate, large drive is usually a good idea.

Resources