English 中文(简体)
Logging from multiple processes to same file using Enterprise Library 4.1
原标题:

I have several processes running concurrently that I want to log to the same file.

We have been using Enterprise Library 4.1 Logging Application Block (with a RollingFlatFileTraceListener), and it works fine, apart from the fact that it prepends a GUID to the log file name when two processes try to write to the log file at the same time (a quirk of System.Diagnostics.TextWriterTraceListener I believe).

I ve tried various things, including calling Logger.Writer.Dispose() after writing to the log file, but it s not ideal to do a blocking call each time a log entry is being written.

The EntLib forums suggest using MSMQ with a Distributor Service, but that is not an option as MSMQ is not allowed at my company.

Is there another way I can quickly and easily log from multiple threads/processes to the same file?

最佳回答

Sorry to say but the answer is no. The File TraceListeners lock the output file so only one TraceListener can log to a file.

You can try other Trace Listeners that are not file based (e.g. Database, Event Log).

Another option I can think of would be to write your own logging service (out of process) that would log to the file and accepts LogEntries. Then create a custom trace listener that sends a message to your service.

It might not be a good idea since you would have a bit of custom development plus it could impact performance since it is an out of process call. Basically you are setting up your own simplified-pseudo-distributor-service.

问题回答

EntLib locks the log file when it writes to it. Therefore, 2 processes cannot write to the same log file.

When we have had this problem, that we needed to log from many difference places, to the same place, we have used database logging.

If you are 100% stuck logging to a text file, then you could log to individual log files, and then write a program to merge these files.

I know this is old, but if you are still curious. log4net supports this:

http://logging.apache.org/log4net/release/faq.html#How do I get multiple process to log to the same file?

The problem occurs when the App Pool Recycles and allows for Overlapping Threads. The closing thread has it still open, and the new thread gets the error. Try disabling the overlapping recycling behavior in IIS, or create your own version of the text writer.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签