开发者

Bad configuration of NLog?

Ok, so I'm trying out NLog, but it keeps falling over. Let me go through what I've got so far.

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">

    <targets>
      <target name="file" xsi:type="File"
            layout="${longdate} ${logger} ${message}"
            fileName="${basedir}/Logs/${shortdate}.log" />
    </targets>

    <rules>
        <logger name="*" minlevel="Debug" writeTo="file" />
    </rules>
</nlog>

Then I'm creating my object like this:

private static Logger logger = LogManager.GetCurrentClassLogger();

And then when I want to log something I'm doing it like this:

try
{   
    // Do something
}
catch (Exception ex)
{
    logger.Fatal("ERROR {0}", ex);
    return null;
}

But I'm getting the following errors:

System.TypeInitializationException was unhandled
Message="The type initializer for 'Rhs.CMSSites.Services.GetBlogFeeds' threw an exception."
Source="GetBlogFeeds"
TypeName="Rhs.CMSSites.Services.GetBlogFeeds"
StackTrace:
   at Rhs.CMSSites.Services.GetBlogFeeds.Main(String[] args)
   at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.开发者_开发知识库Threading.ThreadHelper.ThreadStart()
InnerException: System.NullReferenceException
   Message="Object reference not set to an instance of an object."
   Source="GetBlogFeeds"
   StackTrace:
        at Rhs.CMSSites.Services.GetBlogFeeds.UserDetails(String UserName) in C:\Dev\Rhs.CMSSites.Services\GetBlogFeeds\GetBlogFeeds\Program.cs:line 217
        at Rhs.CMSSites.Services.GetBlogFeeds..cctor() in C:\Dev\Rhs.CMSSites.Services\GetBlogFeeds\GetBlogFeeds\Program.cs:line 25
   InnerException: 

Any ideas?

Edit

Probably should have mentioned this earlier, but this is a command line app. GetBlogFeeds is the name of the class.

Edit2

Ok, I've been an absolute idiot. I was calling the UserDetails() method (which attempts to use the NLog method) before instantiating the logger class. I'm going to upvote you both. Thanks and sorry.


My guess (and it is just that) would be that this is nothing to do with NLog, but is rather that that TypeInitializationException is so serious and/or not occurring in the context of that try ... catch that NLog doesn't get a chance to log it.

If you put a breakpoint on that logger.Fatal statement, does execution stop there when you run with the debugger attached?

What's actually happening in the class ctor for GetBlogFeeds - from the stack trace is looks like you call UserDetails for a specific user, which seems a slightly odd thing to be doing in a class ctor.


Why are you returning null when the exception happens?

Based on the posted exception its because of a null exception, it also of course might be the fact your attempting to log the entire Exception object instead of its Message.

Be sure if you agree with this answer to mark it as the answer. If you have additional question with regards to the posted error I can answer them. Without more information ( for example ) where exactly the error is happening in your code I couldn't provide more information.

I just pointed out two odd behaviors.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜