Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6700575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:49:52+00:00 2026-05-26T06:49:52+00:00

I currently have a project that uses Entity Framework 4.1 for logging to a

  • 0

I currently have a project that uses Entity Framework 4.1 for logging to a database so we can track the web application which is deployed on more than one web server. I built it using Scott Gu’s Code First solution.

So, I have code like this:

logging.Logs.Add(newLog);

which is sometimes throwing this error:

System.NullReferenceException : Object reference not set to an
instance of an object. at
System.Data.Objects.ObjectStateManager.DetectConflicts(IList1
entries) at System.Data.Objects.ObjectStateManager.DetectChanges() at
System.Data.Entity.Internal.Linq.InternalSet
1.ActOnSet(Action action,
EntityState newState, Object entity, String methodName) at
System.Data.Entity.Internal.Linq.InternalSet1.Add(Object entity) at
System.Data.Entity.DbSet
1.Add(TEntity entity)

Most of the time it works fine, but it will hiccup now and then. Is there a best practice I should be aware of when I have more than one server using this code to access/write to the same database?

The approach used right now is that each request causes the system to add several new log objects into the collection and then save a grouping of them, rather than save each individual log record. Here’s an outline of my class.

public class LoggingService : ILoggingService
{
    Logging.Model.MyLogging logging;

    public LoggingService()
    {
        InitializeLog();
    }

    /// <summary>
    /// Save any pending log changes (only necessary if using the Add methods)
    /// </summary>
    public void SaveChanges()
    {
        //ensure that logging is not null
        InitializeLog();

        logging.SaveChanges();
    }

    #region Private Methods
    private void InitializeLog()
    {
        if (logging == null)
            logging = new Model.MyLogging();
    }

    private void Log(Level level, int sourceId, string message, bool save, int? siteId = null, int? epayCustomerId = null, 
        string sessionId = null, int? eventId = null, Exception exception = null)
    {
        if (sourceId == 0)
            throw new ArgumentNullException("sourceId", "The Source Id cannot be null and must be valid.");

        var source = (from s in logging.Sources
                     where s.SourceId == sourceId
                     select s).FirstOrDefault();

        if (source == null)
            throw new ArgumentNullException("sourceId", String.Format("No valid source found with Id [{0}].", sourceId));

        if (eventId.HasValue)
        {
            if (eventId.Value > 0)
            {
                var code = (from e in logging.Events
                            where e.EventId == eventId.Value
                            select e).FirstOrDefault();

                //if event id was passed in but no event exists, create a "blank" event
                if (code == null)
                {
                    Event newCode = new Event()
                    {
                        EventId = eventId.Value, 
                        Description = "Code definition not specified."
                    };

                    InitializeLog();
                    logging.Events.Add(newCode);
                    logging.SaveChanges();
                }
            }
        }

        var newLog = new Log()
        {
            Created = DateTime.Now,
            Message = message,
            Source = source,
            Level = level,
            EventId = eventId,
            SessionId = sessionId,
            SiteId = siteId,
            MachineName = System.Environment.MachineName,
        };

        if (exception != null)
            newLog.Exception = String.Format("{0}{1}{2}{1}", exception.Message, Environment.NewLine, exception.StackTrace);

        //ensure that the logging is not null
        InitializeLog();

        logging.Logs.Add(newLog);

        if (save)
        {
            logging.SaveChanges();
        }
    }

    #endregion

}

I am using IoC with StructureMap, and I did not initialize this class as a singleton.

For<ILoggingService>().Use<LoggingService>();

And my context class looks like this:

internal class MyLogging : DbContext
{
    public DbSet<Source> Sources { get; set; }
    public DbSet<Event> Events { get; set; }
    public DbSet<Log> Logs { get; set; }

    /// <summary>
    /// DO NOT ADD ITEMS TO THIS COLLECTION
    /// </summary>
    public DbSet<LogArchive> LogArchives { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        Database.SetInitializer(new MyDbContextInitializer());

        modelBuilder.Entity<Event>().Property(p => p.EventId)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        modelBuilder.Entity<Source>().Property(p => p.SourceId)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        modelBuilder.Entity<LogArchive>().Property(p => p.LogId)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        base.OnModelCreating(modelBuilder);
    }

    //public class MyDbContextInitializer : DropCreateDatabaseIfModelChanges<MyLogging>
    public class MyDbContextInitializer : CreateDatabaseIfNotExists<MyLogging>
    {
        protected override void Seed(MyLogging dbContext)
        {
            // seed data

            base.Seed(dbContext);
        }
    }
}

I’m probably doing something obviously wrong, but I just don’t see it.

EDIT:
As requested, here is a sample of how I call the logging service code. This particular method is logging information related to the HTTP Request. I append the the log items in one try catch and save in a separate try catch so if there is an issue it will at least save the additions that took. The handler is another service injected into this class through IoC that sends the details of the error to me via e-mail.

A single post to the server could log as many as 50-70 separate details separated into chunks of 10-15 (http request, data sent to a web service, result of the web service call, response back to the client), which is why I want to add a grouping and then save the grouping, rather than open and close a connection with each individual item.

public void LogHttpPostStart(HttpPostRequest request)
{
        try
        {
            //if no session is set, use the ASP.NET session
            request.SessionId = GetSessionId(request.SessionId);
            int eventId = (int)Model.Enums.Logging.Event.SubmittedByClient;

            var current = HttpContext.Current;

            if (current != null)
            {
                logService.AddDebug((int)request.Source, String.Format("{0}  HTTP Request Details  {0}", Header2Token.ToString().PadRight(HeaderTokenCount, Header2Token)),
                    siteId: request.SiteId, epayCustomerId: request.EPayCustomerId, sessionId: request.SessionId,
                    eventId: eventId);

                //Server Information
                logService.AddDebug((int)request.Source, String.Format("Machine Name: {0}", current.Server.MachineName),
                    siteId: request.SiteId, epayCustomerId: request.EPayCustomerId, sessionId: request.SessionId,
                    eventId: eventId);

                //User Information
                logService.AddDebug((int)request.Source, String.Format("User Host Address: {0}", current.Request.UserHostAddress),
                    siteId: request.SiteId, epayCustomerId: request.EPayCustomerId, sessionId: request.SessionId,
                    eventId: eventId);
                logService.AddDebug((int)request.Source, String.Format("User Host Name: {0}", current.Request.UserHostName),
                    siteId: request.SiteId, epayCustomerId: request.EPayCustomerId, sessionId: request.SessionId,
                    eventId: eventId);

                //Browser Information
                if (current.Request.Browser != null)
                {
                    logService.AddDebug((int)request.Source, String.Format("Browser: {0}", current.Request.Browser.Browser),
                        siteId: request.SiteId, epayCustomerId: request.EPayCustomerId, sessionId: request.SessionId,
                        eventId: eventId);
                    logService.AddDebug((int)request.Source, String.Format("Browser Version: {0}", current.Request.Browser.Version),
                        siteId: request.SiteId, epayCustomerId: request.EPayCustomerId, sessionId: request.SessionId,
                        eventId: eventId);
                    logService.AddDebug((int)request.Source, String.Format("User Agent: {0}", current.Request.UserAgent),
                        siteId: request.SiteId, epayCustomerId: request.EPayCustomerId, sessionId: request.SessionId,
                        eventId: eventId);
                    logService.AddDebug((int)request.Source, String.Format("Is Mobile Device: {0}", current.Request.Browser.IsMobileDevice.ToString()),
                        siteId: request.SiteId, epayCustomerId: request.EPayCustomerId, sessionId: request.SessionId,
                        eventId: eventId);

                    if (current.Request.Browser.IsMobileDevice)
                    {
                        logService.AddDebug((int)request.Source, String.Format("Mobile Device Manufacturer: {0}", current.Request.Browser.MobileDeviceManufacturer),
                            siteId: request.SiteId, epayCustomerId: request.EPayCustomerId, sessionId: request.SessionId,
                            eventId: eventId);
                        logService.AddDebug((int)request.Source, String.Format("Mobile Device Model: {0}", current.Request.Browser.MobileDeviceModel),
                            siteId: request.SiteId, epayCustomerId: request.EPayCustomerId, sessionId: request.SessionId,
                            eventId: eventId);
                    }

                    logService.AddDebug((int)request.Source, String.Format("Platform: {0}", current.Request.Browser.Platform),
                        siteId: request.SiteId, epayCustomerId: request.EPayCustomerId, sessionId: request.SessionId,
                        eventId: eventId);
                    logService.AddDebug((int)request.Source, String.Format("Cookies Enabled: {0}", current.Request.Browser.Cookies.ToString()),
                        siteId: request.SiteId, epayCustomerId: request.EPayCustomerId, sessionId: request.SessionId,
                        eventId: eventId);
                    logService.AddDebug((int)request.Source, String.Format("Frames Enabled: {0}", current.Request.Browser.Frames.ToString()),
                        siteId: request.SiteId, epayCustomerId: request.EPayCustomerId, sessionId: request.SessionId,
                        eventId: eventId);

                    if (current.Request.Browser.JScriptVersion != null)
                    {
                        logService.AddDebug((int)request.Source, String.Format("Javascript Version: {0}", current.Request.Browser.JScriptVersion.ToString()),
                            siteId: request.SiteId, epayCustomerId: request.EPayCustomerId, sessionId: request.SessionId,
                            eventId: eventId);
                    }

                }

                logService.AddDebug((int)request.Source, String.Format("{0}  End HTTP Request Details  {0}", Header2Token.ToString().PadRight(HeaderTokenCount, Header2Token)),
                    siteId: request.SiteId, epayCustomerId: request.EPayCustomerId, sessionId: request.SessionId,
                    eventId: eventId);
            }
        }
        catch (Exception ex)
        {
            handler.HandleError(true, ex);
        }

        try
        {
            logService.SaveChanges();
        }
        catch (Exception ex)
        {
            handler.HandleError(true, ex);
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:49:52+00:00Added an answer on May 26, 2026 at 6:49 am

    The first problem is that you are keeping open your DbContext during the application. The best practice is to only instantiate it when needed and then dispose it. But the bigger problem here is that you use your LoggingService in a multithreaded enviroment (if I understood it correctly) but DbContext is NOT thread safe. See this SO question and also the comments on ScottGu’s post (search for the word thread). So you should keep your log entries around in a thread safe way somewhere else not in the DbContext and only open a DbContext when you want to access the DB.

    +1 Instead of the InitializeLog() method check out System.Lazy<T>

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Flex/Java web application that uses Maven as a build tool. Currently,
Hello Ruby/Rails/Merb developers! Im currently working on a web project that will have a
I currently have a project that uses g++ to compile it's code. I'm in
I currently have a C# project which uses plugins and has a fairly common
we are currently working on a rails project that uses i18n and we have
I have a large web project that uses log4j directly, together with many 3rd-party
I'm currently building a .net web application that uses WCF web services to allow
I have a project that I'm currently working on but it currently only supports
I have been trying to create a Spring project that uses MyBatis for the
I am currently working on a project that uses CoreData and relations are using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.