How Thread-Safe is NLog?

后端 未结 6 1952
慢半拍i
慢半拍i 2020-12-02 23:23

Well,

I have waited for days before deciding to post this issue, as I was not sure how to state this, resutling into a long detailed post. However, I think it is re

6条回答
  •  不思量自难忘°
    2020-12-02 23:49

    According to documentation on their wiki they are thread safe:

    Because loggers are thread-safe, you can simply create the logger once and store it in a static variable

    Based on the error you are getting you are most likely doing exactly what the exception sates: modifying the collection somewhere in the code while the foreach loop is iterating. Since your list is passed by reference this isn't hard to imagine where another thread would be modifying the collection of rules while you are iterating over it. You have two options:

    1. Create all your rules up front and then add them in bulk (this is what I think you want).
    2. Cast your list to some read-only collection (rules.AsReadOnly()) but miss any updates that happen as threads you've spawned create new rules.

提交回复
热议问题