C
C#3w ago
Faker

Trying to log database operations into file and insert values into db using ef core at same time

Processing failed: The process cannot access the file 'C:\Users\user\RiderProjects\TestMigrations\driving-school-booking-system\MainProject\MainProject\Logs\logs.txt' because it is being used by another process.
Processing failed: The process cannot access the file 'C:\Users\user\RiderProjects\TestMigrations\driving-school-booking-system\MainProject\MainProject\Logs\logs.txt' because it is being used by another process.
Hello guys, I'm trying to read from a file and inserting into my database, which I was able to do. The problem is since database is already in used, I'm not being able to log the operations in my log file. So I was wondering what is the issue here, is it some kind of threading issue where same thread can't access database more than once and we can resolve it using some kind of multi-threading ?
6 Replies
Faker
FakerOP3w ago
hmm edit: I think the issue is I'm trying to write to a file (log file) that hasn't been disposed/closed yet. Is there a way to handle the issue where 2 operations can write to the file at the same time ?
Cracker
Cracker3w ago
It's not possible to write into the same file concurrently You can have a in-memory pool like ConcurrentBag to put logs into and a single consumer can read and write into text file from the pool. It can do like bulk write into file once the count is like 50 or whatever is proper for you There might be better ways to implement pooling so take a look at that separately
Faker
FakerOP3w ago
I read there is an option in filestream, fileshare, don't know if this can be used basically it's like multiple people writing to same file at the same time, does it matter the order in which we write things in the log file ?
Cracker
Cracker3w ago
You can share the filestream instance but it probably won't work well on multi thread environments or concurrent operations
Faker
FakerOP3w ago
ok, ty in a log file, does it matter the order in which we log the sql commands? I mean, I think it matters, no? like we don't want to mix unrelated sql commands; this is what filestream fileshare can do ?
Cracker
Cracker3w ago
Stream and log data you have separated things. You use stream to write text to file. Whatever you write is depends on your needs

Did you find this page helpful?