c# - Store DBContext Log in other EF table -
i want store logs in db table (imported ef code).
my code looks this:
public pmcontext() : base("myentities") { configuration.proxycreationenabled = false; configuration.lazyloadingenabled = false; this.database.log = s => logstore(s); } private void logstore(string message) { tlog log = new tlog(); log.description = message; log.insertdate = datetime.now; logs.add(log); savechanges(); } public virtual dbset<tlog> logs { get; set; }
i cannot use savechanges() (get error "sqlconnection not support parallel transactions") , if remove line nothing stored in db table.
how can fix this?
i think happens during savechanges
call anywhere in app, because sql generated call logged, triggers nested savechanges
call tries start own transaction.
and there thing: savechanges
call in logstore
method trigger logging itself, you'd end starting infinite loop if worked.
if want through context, better use separate instance logging. instance should not logging:
public pmcontext() : this(true) { } private pmcontext(bool dologging) : base("myentities") { configuration.proxycreationenabled = false; configuration.lazyloadingenabled = false; if (dologging) { this.database.log = s => logstore(s); } } private void logstore(string message) { using(var db = new pmcontext(false)) { tlog log = new tlog(); log.description = message; log.insertdate = datetime.now; db.logs.add(log); db.savechanges(); } }
probably better through async methods, because synchronous execution affects database interactions. better option through log4net, using database logger.
Comments
Post a Comment