c# - Sharing violation on path for my logmanager -
this question has answer here:
- sharing violation on path error c# 2 answers
i'm using code saving logs in may c# monotouch application:
public static void writeexeption(string message){ string path= storageclass .logspath ; string filepath= path.combine (path ,"log.txt"); if(!file.exists (filepath )){ using (streamwriter sw = file.createtext(filepath)) { sw.writeline ("--------------------------------------------------------------------------" + "--------------------"); sw.writeline("blahblah..."); sw.writeline ("--------------------------------------------------------------------------" + "--------------------"); } } using (streamwriter w = file.appendtext(filepath )) { log(message , w); } } public static void log(string logmessage, textwriter w) { w.write("\r\nlog entry : "); w.writeline("{0} {1}", datetime.now.tolongtimestring(), datetime.now.tolongdatestring()); w.writeline(" :"); w.writeline(" :{0}", logmessage); w.writeline ("--------------------------------------------------------------------------" + "--------------------"); } but in application error:
sharing violation on path 'file path'
try adding enclose streamwriter appends data in lock statement. first, add object refer to:
static object _locker = new object(); then, modify writeexeption method locking last using statement:
lock (_locker) { using (streamwriter w = file.appendtext(filepath)) { log(message, w); } } if still doesn't work, means other application using file. if works, means logging on multiple threads.
Comments
Post a Comment