locked
Log4Net object reference not set ? RRS feed

  • Question

  • User442275730 posted

    I am getting a strange error when using log4net but in my web app its crashing saying object not created. I am unitizing the logger with the following

    log4net.ILog logger = log4net.LogManager.GetLogger(typeof(_default));
    

     

      <log4net>
      <appender name="LogFileAppender" type="log4net.Appender.FileAppender">
              
        <param name="File" value="C:\Users\David.Buckley\Documents\Visual Studio 2015\Projects\ApertureNet\ApertureNetSolution\ApertureNet\ApertureNet\logs\ApertureNet.log" />
        <!--<param name="AppendToFile" value="true"/>-->
          <layout type="log4net.Layout.PatternLayout">
      <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
                </layout>
            </appender>
     
     
            <root>
                <level value="All" />
                <appender-ref ref="LogFileAppender" />
            </root>
    </log4net>
      

    Error

     Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    
    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
    
    Source Error:
    
    
    Line 105:            catch (Exception EX)
    Line 106:            {
    Line 107:                logger.Error("Error in bookApointmentClick in main Calender Booking Form " +EX.InnerException.ToString());
    Line 108:
    Line 109:            }

    E

     protected void bookApointment_Click(object sender, EventArgs e)
            {
                try
                {
                    Appointment _appointment = new Appointment();
                    int id = Convert.ToInt32(Request.QueryString["id"]);
    
                    _appointment = _dal.GetAppointMentByiD(id, "");
                    _customRef = _dal.GetPrefix("Calendar").ToString();
    
                    if (txtRef.Text != "")
                        _appointment.TimeCode = _customRef;
    
                    FormsIdentity _identity = (FormsIdentity)Context.User.Identity;
                    _identity = (FormsIdentity)Context.User.Identity;
    
                    User _myuser = _dal.getUser(_identity.Name.ToString());
                    string advisorName = _myuser.FirstName + " " + _myuser.LastName;
    
                    DateTime startDate = (!rdStarDate.SelectedDate.HasValue || rdStarDate.SelectedDate.Value == new DateTime(1900, 1, 1)) ? DateTime.Today : rdStarDate.SelectedDate.Value;
                    DateTime endDate = (!rdEndDate.SelectedDate.HasValue || rdEndDate.SelectedDate.Value == new DateTime(1900, 1, 1)) ? DateTime.Today : rdEndDate.SelectedDate.Value;
                    _appointment.TimeCode = _customRef;
    
                    _appointment.CustomerFirstName = txtFirstname.Text;
                    _appointment.CustomerLastName = txtLastName.Text;
                    _appointment.Start = startDate;
                    _appointment.End = endDate;
                    _appointment.Subject = "Client Name: " + txtFirstname.Text + " " + txtLastName.Text + " Senior Manager : " + rdManagers.SelectedItem.Text.ToString() + " Advisor Name: " + Master.userName.ToString() + " Prefered Contact Number:" + txtContactNum.Text + " Notes:" + txtNotes.Text;
                    _appointment.isDeleted = false;
                    _appointment.notes = txtNotes.Text;
    
                    _appointment.preferedContactNumber = txtContactNum.Text;
                    _appointment.mobileNumber = txtMobileNumber.Text;
                    _appointment.authUserName = Environment.UserName;
                    _appointment.authCreatedDate = DateTime.Now;
    
                    string manager = rdManagers.SelectedItem.Text;
    
                    if (chkEmail.Checked == true)
                        sendAppointmentEmails(txtEmail.Text, new Guid(Constants.calenderEmail), txtFirstname.Text, txtLastName.Text, manager, txtContactNum.Text, txtEmail.Text, startDate, endDate);
    
                    tblApertureNetNotification _notification = new tblApertureNetNotification();
    
                    _notification = _dal.GetNotificationById(Constants.smsNotification);
                    string bodyMessage = _notification.body;
    
                    Dictionary<string, string> stringsToReplace = new Dictionary<string, string>();
                    stringsToReplace.Add("{christian_name}", txtFirstname.Text);
                    stringsToReplace.Add("{first_name}", txtLastName.Text);
    
                    stringsToReplace.Add("{surname}", txtLastName.Text);
                    stringsToReplace.Add("{advisor_name}", rdManagers.SelectedItem.Text);
                    stringsToReplace.Add("{customer_phone}", txtContactNum.Text);
                    stringsToReplace.Add("{customer_email}", txtEmail.Text);
                    stringsToReplace.Add("{customer_ref}", txtRef.Text);
                    stringsToReplace.Add("{appointment_datetime}", startDate.ToString("dddd,dd,MMMM, yyyy") + " at " + startDate.ToShortTimeString());
                    stringsToReplace.Add("<br>", "");
                    stringsToReplace.Add("<br />", Environment.NewLine);
                    stringsToReplace.Add("<p>", "");
                    stringsToReplace.Add("</p>", Environment.NewLine);
                    stringsToReplace.Add("&nbsp;", "");
                    bodyMessage = bodyMessage.MultipleReplace(stringsToReplace);
    
                    _sms.send("ApertureIVA", txtMobileNumber.Text, bodyMessage, "", "ADVICE", "ADV_5799");
    
                    if (_appointment.EntityState == System.Data.EntityState.Detached)
                        _dal.apertureNetEntities.Appointments.AddObject(_appointment);
    
                    _dal.SaveChanges();
                    Response.Redirect("thankyou.aspx");
                }
                catch (Exception EX)
                {
                    logger.Error("Error in bookApointmentClick in main Calender Booking Form " +EX.InnerException.ToString());
    
                }
    }

    Saturday, March 12, 2016 9:39 PM

Answers

  • User-986267747 posted

    Hi david38ni,

    I am getting a strange error when using log4net but in my web app its crashing saying object not created.

    According to your description, i'm not sure what line of code you have caused the problem. So please debug your code and find the line caused this problem in your code.

    <param name="File" value="C:\Users\David.Buckley\Documents\Visual Studio 2015\Projects\ApertureNet\ApertureNetSolution\ApertureNet\ApertureNet\logs\ApertureNet.log" /> <!--<param name="AppendToFile" value="true"/>--> <layout type="log4net.Layout.PatternLayout"> <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" /> </layout> </appender> <root> <level value="All" /> <appender-ref ref="LogFileAppender" /> </root> </log4net>

    It seems that the use method of the log4net is not right. You could refer to the following links to learn the correct way of using log4net.

    http://stackoverflow.com/questions/7089286/correct-way-of-using-log4net

    http://www.codeproject.com/Articles/140911/log-net-Tutorial

    I hope it's helpful to you.

    Best Regards,

    Klein zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 14, 2016 2:16 AM