English 中文(简体)
How to set Quartz.net worker thread name?
原标题:

How can I set name of the worker thread in Quartz.net?

[2009-12-15 08:56:25] [DefaultQuartzScheduler_Worker-1] INFO

I wanted to see some meaningful information in the logs. I tried using JobDetail constructor, but looks like I am wrong.

var job = new JobDetail("n1", null, typeof (MyJob));
最佳回答

Atleast there is no way to set the name of the worker thread. However, I have used %property facility of log4net to produce meaningful thread name in log files

问题回答

Usually you would be interested in what jobs output (they should have their own loggers) and not interested in the thread.

Do you have a specific case where you need logical names for threads? The threads are pooled and there are no guarantees about which thread gets what kind of job to to process. That s why the thread name usually is usable only for debug purposes to track the things happened in specific thread s life-cycle.

This is a new answer to an old question, but maybe its helpful to someone.

I didn t like the long quartz thread name [DefaultQuartzScheduler_Worker-1] in my log files.

Instead I wanted log4net to show the thread id. Therefor you need to set a property that logs the current thread s id.

I found this class. source
This is a lambda / func way of logging calculated context: log4net context explained

public class Log4NetContextProperty : IFixingRequired
{
    private readonly Func<string> _getValue;

    public Log4NetContextProperty(Func<string> getValue)
    {
        _getValue = getValue;
    }

    public override string ToString()
    {
        return _getValue();
    }

    public object GetFixedObject()
    {
        return ToString();
    }
}

And you have to call it this way in your application start place.

log4net.GlobalContext.Properties["threadId"] = new Log4NetContextProperty(() => Thread.CurrentThread.ManagedThreadId.ToString());

Then in your log4net config adapt the conversionpattern to include the new property. Mine looks like this:

<conversionPattern value="%d{dd.MM.yyyy HH:mm:ss,ffff} %-5level [%property{threadId}] - %message%newline" />

That s it. This way the id of each thread will be determinded at runtime and logged in its own property.





相关问题
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. ...

热门标签