NLog 'Out of the box' throws exception on Win7 Visual Studio 2010

267 views
Skip to first unread message

Patrick Skelton

unread,
Mar 20, 2013, 11:34:12 AM3/20/13
to nlog-...@googlegroups.com
Hi,

I am new to NLog.  To try it out, I have just created a new ASP.NET 4 project in Visual Studio 2010 and added a single web page and NLog.  I set the web site to use the inbuilt development server, which has a virtual directory with IIS_IUSRS set to Full control.  In the Page_Load of the single page, I made calls to NLog using the 'normal' method, i.e.

protected void Page_Load( object sender, EventArgs e )
{
_log.Debug( "Message at Debug level" );
}

private static Logger _log = LogManager.GetCurrentClassLogger();

I got no output, so I changed the configuration file to throw exceptions to see if I could spot what was failing.  So now my configuration is:

<?xml version="1.0" encoding="utf-8" ?>
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" throwExceptions="true">

<targets>
  <target name="file" xsi:type="File" fileName="${basedir}/log.txt" />
</targets>
<rules>
  <logger name="*" minlevel="Debug" writeTo="file" />
</rules>
</nlog>

I also set Visual Studio to stop on .NET exceptions.  It now fails with the following exception:

System.Security.Permissions.EnvironmentPermission

Can anyone spot what I am doing wrong.  I can't see anything I can strip away to get this working.

Note that IIS is set to Medium Trust because this is what my web hosting provider uses.

Any help or advice would be very much appreciated.

Kind wishes, Patrick

Kim Christensen

unread,
Mar 20, 2013, 3:32:21 PM3/20/13
to nlog-...@googlegroups.com
Hi Patrick,

There are some issues when using NLog in medium trust, which aren't fully handle. I'll try and take a look at what is causing this issue.

Patrick Skelton

unread,
Mar 20, 2013, 4:21:57 PM3/20/13
to nlog-...@googlegroups.com
That's great, Kim!  Thank you.

Kim Christensen

unread,
Mar 20, 2013, 4:31:19 PM3/20/13
to nlog-...@googlegroups.com
Hi Patrick,

I've taken a look at the issue.I can't seem to get the exception you are getting, but when I enable medium trust nothing will be logged, so it seems to be an issue when running in medium trust


Den onsdag den 20. marts 2013 16.34.12 UTC+1 skrev Patrick Skelton:

Patrick Skelton

unread,
Mar 20, 2013, 4:50:28 PM3/20/13
to nlog-...@googlegroups.com
Hi, Kim,

Depending on the settings I make in the NLog.config, I have had different exceptions (e.g. when I tried to enable the internal logging file).

- Patrick

Kim Christensen

unread,
Mar 21, 2013, 8:45:54 AM3/21/13
to nlog-...@googlegroups.com
What exception are you getting with the configuration you posted?

Patrick Skelton

unread,
Mar 21, 2013, 9:44:43 AM3/21/13
to nlog-...@googlegroups.com
Hi, Kim,

I get the one I mentioned earlier:

System.Security.Permissions.EnvironmentPermission

Perhaps I should mention that the only way I see these exceptions is by getting Visual Studio to stop when they are thrown.  If I leave the default setting of stopping only on user-unhandled exceptions, I get no errors; I simply get no logging, as you have found yourself.

Oddly, the suppressInternalErrors setting in NLog.config doesn't seem to have an effect, no matter what I set it to.  Maybe the exceptions are happening on another thread.  I don't know - I haven't looked into it that deeply.

- Patrick

Kim Christensen

unread,
Mar 21, 2013, 10:39:44 AM3/21/13
to nlog-...@googlegroups.com
I've tried to enable throwing the internal exceptions instead of suppressing them, and I get no exception at all.

Patrick Skelton

unread,
Mar 21, 2013, 12:02:11 PM3/21/13
to nlog-...@googlegroups.com
Do you get no exceptions if you set Visual Studio to break when .NET exceptions are thrown?

Kim Christensen

unread,
Mar 23, 2013, 7:04:02 PM3/23/13
to nlog-...@googlegroups.com
Not I my current setup. What does the exception message say?

Patrick Skelton

unread,
Mar 24, 2013, 6:30:07 AM3/24/13
to nlog-...@googlegroups.com
Hi, Kim,

This is the exception copied to the clipboard:

System.Security.SecurityException occurred
  Message=Request for the permission of type 'System.Security.Permissions.EnvironmentPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
  Source=mscorlib
  StackTrace:
       at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
       at System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap, StackCrawlMark& stackMark)
       at System.Security.CodeAccessPermission.Demand()
       at System.Environment.GetEnvironmentVariable(String variable)
       at NLog.Common.InternalLogger.GetSettingString(String configName, String envName)
  InnerException: 

If this doesn't help, I could zip up the solution and send it to you.  As I say, if you are running Windows 7 with IIS 7.5, you should get the same result as me because I have done nothing 'special' to the solution; it even uses the default built-in development server.

- Patrick


--
You received this message because you are subscribed to a topic in the Google Groups "NLog-Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nlog-users/NX88CoO9MVs/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to nlog-users+...@googlegroups.com.
To post to this group, send email to nlog-...@googlegroups.com.
Visit this group at http://groups.google.com/group/nlog-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Kim Christensen

unread,
Mar 26, 2013, 4:50:44 AM3/26/13
to nlog-...@googlegroups.com
It seems like it is a problem related to Medium Trust, as NLog tries to read some environment variables, as long as you don't need the environment variables to be read, there shouldn't be any problems.
Are you setting NLog up in the web.config or NLog.config?

Patrick Skelton

unread,
Mar 26, 2013, 11:21:19 AM3/26/13
to nlog-...@googlegroups.com
Hi, Kim,

As far as I know, I don't care about environment variables, but the problem is that I get no log files written.

I have been using an NLog.config file.  I didn't know you could use a section inside the Web.config file.  Do you think that would make any difference, and if so, could you point me at an example?

Sorry to take so much of your time with this.


- Patrick

Kim Christensen

unread,
Mar 26, 2013, 11:25:38 AM3/26/13
to nlog-...@googlegroups.com

I've seen some problems with using the NLog configuration file when running in medium trust, especially when running in IIS. I would recommend you to try using the web.config

You received this message because you are subscribed to the Google Groups "NLog-Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nlog-users+...@googlegroups.com.

Patrick Skelton

unread,
Mar 26, 2013, 11:30:44 AM3/26/13
to nlog-...@googlegroups.com
Are there any examples of this anywhere, Kim?

Kim Christensen

unread,
Mar 26, 2013, 11:42:50 AM3/26/13
to nlog-...@googlegroups.com

Sorry, forgot to include link, you can find information here, https://github.com/NLog/NLog/wiki/Configuration-file

Patrick Skelton

unread,
Mar 26, 2013, 12:16:05 PM3/26/13
to nlog-...@googlegroups.com
Thanks, Kim.  Unfortunately, I get exactly the same exception using the Web.config file:

System.Security.SecurityException occurred
  Message=Request for the permission of type 'System.Security.Permissions.EnvironmentPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
  Source=mscorlib
  StackTrace:
       at System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet)
       at System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap, StackCrawlMark& stackMark)
       at System.Security.CodeAccessPermission.Demand()
       at System.Environment.GetEnvironmentVariable(String variable)
       at NLog.Common.InternalLogger.GetSettingString(String configName, String envName)
  InnerException: 

- Patrick

Kim Christensen

unread,
Mar 26, 2013, 12:17:27 PM3/26/13
to nlog-...@googlegroups.com

I would suspect that, and still no log?

Patrick Skelton

unread,
Mar 26, 2013, 1:41:12 PM3/26/13
to nlog-...@googlegroups.com
If I keep hitting F5 to go through the exceptions, I get multiple security exceptions and then I eventually get the standard ASP.NET error page, which says:

[SecurityException: Request for the permission of type 'System.Configuration.ConfigurationPermission, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' failed.]
   System.Security.CodeAccessSecurityEngine.Check(Object demand, StackCrawlMark& stackMark, Boolean isPermSet) +0
   System.Security.CodeAccessSecurityEngine.Check(CodeAccessPermission cap, StackCrawlMark& stackMark) +31
   System.Security.CodeAccessPermission.Demand() +46
   System.Configuration.BaseConfigurationRecord.CheckPermissionAllowed(String configKey, Boolean requirePermission, Boolean isTrustedWithoutAptca) +99



And, no - still no logs.

- Patrick

Kim Christensen

unread,
Mar 27, 2013, 5:44:35 PM3/27/13
to nlog-...@googlegroups.com
Could you try and enable the internal logging mechanism, as described here https://github.com/NLog/NLog/wiki/Logging-Troubleshooting?

Patrick Skelton

unread,
Mar 28, 2013, 11:05:50 AM3/28/13
to nlog-...@googlegroups.com
Hi, Kim,

I still get the same SecurityException.  I get no internal log file written.  Setting internalLogToConsole="true" gives the following:

'WebDev.WebServer40.EXE' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\aa6cd7db\bb48addd\assembly\dl3\66b6d362\00e07004_d944cc01\NLog.DLL', Symbols loaded.
'WebDev.WebServer40.EXE' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Runtime.Serialization\v4.0_4.0.0.0__b77a5c561934e089\System.Runtime.Serialization.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
The thread '<No Name>' (0x938) has exited with code 0 (0x0).
The thread '<No Name>' (0x1694) has exited with code 0 (0x0).
The program '[4084] iexplore.exe: Script program' has exited with code 0 (0x0).
The program '[2276] WebDev.WebServer40.EXE: Managed (v4.0.30319)' has exited with code 0 (0x0).

Not sure this helps.

- Patrick

Kim Christensen

unread,
Mar 29, 2013, 6:58:08 PM3/29/13
to nlog-...@googlegroups.com
Have you tried to remove the throwExceptions part of the configuration?

Patrick Skelton

unread,
Mar 30, 2013, 5:40:18 AM3/30/13
to nlog-...@googlegroups.com
Sorry, Kim - maybe I have tried some things since I we started this exchange.  The config I am using is already set not to throw exceptions.  I still eventually get the SecurityException below and no log file generated.


- Patrick

Kim Christensen

unread,
Mar 30, 2013, 9:51:40 AM3/30/13
to nlog-...@googlegroups.com
Does it work if IIS is set to Full trust?

Patrick Skelton

unread,
Mar 30, 2013, 2:22:53 PM3/30/13
to nlog-...@googlegroups.com
Yes, everything works fine on Full Trust.




On 30 March 2013 13:51, Kim Christensen <kimwo...@gmail.com> wrote:
Does it work if IIS is set to Full trust?

--

Kim Christensen

unread,
Mar 30, 2013, 3:19:17 PM3/30/13
to nlog-...@googlegroups.com

I'm currently trying to reproduce the error, but right now I'm getting partial trust exception instead. Are NLog installed in the GAC in your machine?

Kim Christensen

unread,
Mar 31, 2013, 5:05:52 AM3/31/13
to nlog-...@googlegroups.com

Seems like it also could be something around the .NET version, i get different exceptions depending on the version. What version are you using?

Patrick Skelton

unread,
Mar 31, 2013, 5:18:57 AM3/31/13
to nlog-...@googlegroups.com
Hi, Kim,

I'm no expert with gacutil.exe, but when I run the application and allow it to get as far as the standard ASP.NET exception page then examine the cache using gacutil /l, it is not listed.  So I am assuming the answer is 'No'.


- Patrick



On 30 March 2013 19:19, Kim Christensen <kimwo...@gmail.com> wrote:

I'm currently trying to reproduce the error, but right now I'm getting partial trust exception instead. Are NLog installed in the GAC in your machine?

--

Patrick Skelton

unread,
Mar 31, 2013, 5:20:13 AM3/31/13
to nlog-...@googlegroups.com
I am using V4, which temporarily (so I can debug static constructors) is using the Classic Pipeline in IIS, rather than the newer Integrated Pipeline.



On 31 March 2013 10:05, Kim Christensen <kimwo...@gmail.com> wrote:

Seems like it also could be something around the .NET version, i get different exceptions depending on the version. What version are you using?

--
Reply all
Reply to author
Forward
0 new messages