2

I'm developing an app. using Common.Logging (http://netcommon.sourceforge.net/index.html). A simplified logging configuration (which is done in web.config) is as follows:

<configuration>
   ...
   <arg key="configType" value="FILE" />
   <arg key="configFile" value="NLog.config" />
   ...
</configuration> 

As you can see here, the configuration points another configuration file (here, NLog.config) of a backend logging framework.

My question is: when deploying in Azure, what path should I specify here (on dev, NLog.config is copied when building the solution, and placed in the bin directory). In other words, what would be the SAFEST way to get the physical place where the app is deployed in Azure?

Thanks in advance!

soleiljy
  • 1,121
  • 2
  • 13
  • 20

1 Answers1

2

In code you can find the current path to the application using Server.MapPath("/"). Now, you can simply make sure that the NLog.config file gets deployed to the application folder:

  1. Add NLog.config to your project (in the root of your web application)
  2. Change the Build Action to Content

In order to test this you can right click on your Azure project an choose Package. In the bin\Release|Debug\app.publish folder of your Azure project you'll find a *.cspkg file. Add a .zip extension to this file and open the file with WinRAR/ZIP/7zip/... Then you'll see a file like this one: SomeWebRole_1a91f39a-49b7-4ece-873f-862172a2fa06.cssx. Here again, add the .zip extension to this file and open it.

If you navigate to the sitesroot\0 folder you'll see the files of the web application in IIS, including the NLog.config file. This way, you' can simply reference the NLog.config file in the Common.Logging settings:

enter image description here

Sandrino Di Mattia
  • 24,739
  • 2
  • 60
  • 65
  • Is it safe/Allowed to use Server.MapPath() in Azure? – Geethanga Sep 03 '12 at 09:00
  • If you call it in your web application yes. Calling this from WebRole.cs or WorkerRole.cs is not possible since these processes don't run in IIS. Note that you can only use that path to read files from. If you want to write files, don't store them under that path but use a LocalResource instead. – Sandrino Di Mattia Sep 03 '12 at 09:37
  • Yes I got this LocalResource covered already. But generally I had the idea of not referring local paths in Azure. Because we have no guarantee about the server configuration which will serve our subsequent requests. I have seen in several videos where they strictly say not use these kinds of paths. – Geethanga Sep 03 '12 at 10:17
  • That's true, but it doesn't apply to what we're doing here. The NLog.config file is included in the service package, this means it will be deployed on all instances and it will be available on all instances. For all data that didn't come with the service package, you are correct and you can't really know on which instance the file would be. – Sandrino Di Mattia Sep 03 '12 at 10:26
  • Thanks for your comments. one more question. is it possible to use Server.MapPath() in Web.config. I guess it's not. Then how can I specify the right path of NLog.config in web.config? – soleiljy Sep 03 '12 at 21:27
  • If you put NLog.config in the root of your project (like the web.config) you shouldn't specify the path. – Sandrino Di Mattia Sep 04 '12 at 00:04
  • I tried, but got an error. The error messages says: NLog configuration file 'NLog.config' does not exists. – soleiljy Sep 04 '12 at 01:33
  • Could you check the package to see if NLog.config is included? – Sandrino Di Mattia Sep 04 '12 at 07:07