Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using NLog, database logging, i am getting Object reference not set to an instance of an object

Posted on 2008-10-02
3
Medium Priority
?
2,066 Views
Last Modified: 2012-06-21
Hi,

I am using NLog to log my messages to the Database. I have already configured the NLog.dll.nlog inside the bin folder where i have the NLog.dll file. Following is the configuration that i have for NLog.dll.nlog.

<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        throwExceptions="true">
      <targets>
            <target name="database" xsi:type="AsyncWrapper" batchSize="100" queueLimit="25000"
                        overflowAction="Grow" timeToSleepBetweenBatches="50">
                  <target xsi:type="RetryingWrapper" retryCount="10" retryDelayMilliseconds="100">
                        <target xsi:type="Database" dbProvider="mssql" useTransactions="false" keepConnection="true"
                                    dbHost="XXXXX" dbDatabase="XXXXX" dbUserName="XXXXX" dbPassword="XXXXX"
                                    commandText="insert into LogMessages
                                          (TimeStamp, [Level], LoggerName, SourceType, SourceFile, SequenceNumber,
                                                SourceApplication, SourceMachine, Text, Thread, SourceMethod, StackTrace, Exception)
                                                values (@TimeStamp, @Level, @LoggerName, @SourceType, @SourceFile,
                                                @SequenceNumber, @SourceApplication, @SourceMachine, @Text, @Thread,
                                                @SourceMethod, @StackTrace, @Exception)" >

                              <parameter name="@TimeStamp" layout="${date}"/>
                              <parameter name="@Level" layout="${level}"/>
                              <parameter name="@LoggerName" layout="${logger}"/>
                              <parameter name="@SourceType" layout="${processinfo}"/>
                              <parameter name="@SourceFile" layout="${threadname}"/>
                              <parameter name="@SequenceNumber" layout="${counter}"/>
                              <parameter name="@SourceApplication" layout="${processname}"/>
                              <parameter name="@SourceMachine" layout="${machinename}"/>
                              <parameter name="@Text" layout="${message}"/>
                              <parameter name="@Thread" layout="${threadid}"/>
                              <parameter name="@SourceMethod" layout="${callsite}"/>
                              <parameter name="@StackTrace" layout="${stacktrace}"/>
                              <parameter name="@Exception" layout="${exception}"/>
                        </target>
                  </target>
            </target>
      </targets>
      <rules>
            <logger name="*" minlevel="Fatal" writeTo="database"/>
      </rules>
</nlog>

=================================================
Now following is the code to call the NLog from my aspx page


private static Logger logger = LogManager.GetCurrentClassLogger();
    protected void Page_Load(object sender, EventArgs e)
    {

       
    }
    protected void but_Click(object sender, EventArgs e)
    {
        logger.Debug("rajan");

        logger.Fatal("sending log message to database from the we app");
        logger.FatalException("rajan", new Exception());
        NLog.LogManager.GetCurrentClassLogger().Factory.Flush(); //It fails here, if i remove this, no
                                                                                                           //llogging happens
    }
}

===================================================
As far as aspx page is concerned, i have wrapped up the button inside the AJAX UploadPanel to make asynchronous request. When i click on Button, and it reaches to Logger.Fatal or FatalException, it gives me "Object Reference not set to an instance of an object".

One thing i would like to add is that the same application i tested with Concole application (offcourse without button click and it logs into the database. It only fails with the Web app.

Is there any one who can help on this ASAP.


Please  do let me knwo if any other info is required on this case.
Rajan
Following is the stack trace
===============================
 
   at NLog.LogFactory.Flush()
   at _Default.but_Click(Object sender, EventArgs e) in e:\Rajan\vs2005\NlogDBTesting\Default.aspx.cs:line 28
   at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Open in new window

0
Comment
Question by:rajan_thakur78
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
3 Comments
 

Author Comment

by:rajan_thakur78
ID: 22627187
I debug both the web app and console app and i found that in case of web app the Configuration file is not loaded. Please see the attached files.

One file is for console application where configuration file is loaded
second file is for the web app where configuration is not loaded.
nlog-console-config-.JPG
nlog-web-config-null.JPG
0
 

Author Comment

by:rajan_thakur78
ID: 22627212
Please see
inside the console app logger.factory.configuration has the config app
inside the web app logger.factory.configuration is null.

According to the documentation on http://www.nlog-project.org/config.html it is clearly said that the Nlog.dll.nlog file should be inside the folder where the NLog.dll file resides. This is what i have in my app.

I am keeping my NLog.dll.nlog inside the bin folder of my web application and same for the console application.
0
 

Accepted Solution

by:
rajan_thakur78 earned 0 total points
ID: 22627300
Ok...

Here we go...

solved...I took the NLog.dll.nlog out of the bin folder, in case of web app, and placed in the directory we we have the web.config, and it is logging now.

Some kinda wierd as the NLog documentation says that the NLog.dll.nlog can be either in  side the folder where the NLog.dll is or at  some other location as per documentation.

So i did place the NLog.dll.nlog inside the bin folder where i had my NLog.dll was, but it gave me null for the configuration.

Anyhow....its resolved.

But i would be glad to know why it happend. I would definitly log this with NLog.

If anybody has faced this, any comment would be really appreciated.

Rajan
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question