简体   繁体   English

WCF服务-记录呼叫者详细信息

[英]WCF Service - Logging Caller Details

I have created a Windows Service (.Net 4.0) that exposes WCF service end points over TCP bindings. 我创建了Windows服务(.Net 4.0),该服务通过TCP绑定公开WCF服务端点。

When a service method is called, it will perform several tasks in separate threads. 调用服务方法时,它将在单独的线程中执行多个任务。 Each class performing the actions has a Log4Net ILog implementation injected in (using dependency injection). 每个执行动作的类都有一个Log4Net ILog实现注入(使用依赖项注入)。

What I would like to do is every log entry to have the IP address of the caller so that I can track a call through or identify which call has errored on the server. 我想做的是每个日志条目都具有呼叫者的IP地址,以便我可以跟踪呼叫或确定服务器上哪个呼叫出错。

Using log4Net's LogicalThreadContext, I have managed to achieve this for any log message written on the same thread but not for anything on other threads. 使用log4Net的LogicalThreadContext,我设法对写在同一线程上的任何日志消息实现了这一点,但对其他线程上的任何消息都没有实现。

Sample code below. 下面的示例代码。

Service Interface: 服务接口:

    [ServiceContract(Namespace = "http://tremac")]
    public interface IBroadcastService
    {
        [OperationContract]
        void PublishMessage(Message message);
    }

Service Implementation: 服务实施:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
class BroadcastService: IBroadcastService
{
    private readonly ILog _logger;


    public BroadcastService(ILog logger)
    {
        _logger = logger;
        _logger.Debug("Broadcast Service Initiated");
    }

    public void PublishMessage(Message message)
    {


        var prop =
                OperationContext.Current.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as
                    RemoteEndpointMessageProperty;
        string ip = prop == null ? "" : prop.Address;

        log4net.LogicalThreadContext.Properties["CallerIp"] = ip;

        try
        {

            _logger.Info("Publishing message from " + message.Sender);
            var sinks = ObjectFactory.GetAllInstances<IMessageWriteRepository>();
            Parallel.ForEach(sinks, x => x.SaveMessage(message));
            _logger.Info("Message published");
        }
        catch (Exception ex)
        {
            _logger.Error("Issue publishing message from " + message.Sender + " on " + ip, ex);
            throw new FaultException("Message was either not published or only partially published. Please contact support");
        }
    }

}

Note the use of Parallel.Foreach where there are several implementations of IMessageWriteRepository being used in parallel. 注意在并行使用IMessageWriteRepository的几种实现的情况下使用Parallel.Foreach。 Each of the implementations again has an ILog injected in via Dependency Injection. 每个实现又通过依赖注入注入了一个ILog。 It appears to be at this point that the context of the caller is lost. 似乎在这一点上,调用者的上下文已丢失。

The pattern layout in my Log4Net appender looks like this: 我的Log4Net附加程序中的模式布局如下所示:

    <conversionPattern value="%date [%-5level] [%thread] [%class] [%property{CallerIp}] %message%newline" />

Finally, the resulting log looks like this: 最后,生成的日志如下所示:

    2015-06-23 09:49:11,004 [INFO ] [13] [BroadcastService] [127.0.0.1] Publishing message from Graeme on 127.0.0.1
    2015-06-23 09:49:11,073 [INFO ] [13] [RabbitMqMessageWriter] [(null)] Sending message on topic TestTopic
    2015-06-23 09:49:11,073 [INFO ] [15] [DatabaseAndFileshareWriteMessageRepository] [(null)] Saving message body to disk
    2015-06-23 09:49:11,104 [INFO ] [15] [DatabaseAndFileshareWriteMessageRepository] [(null)] Saving message meta to database
    2015-06-23 09:49:11,737 [INFO ] [13] [BroadcastService] [127.0.0.1] Message published

Note the [(null)] where the caller IP should be on the log messages for different threads. 请注意[(null)],其中调用者IP应该在不同线程的日志消息中。

Any suggestions welcome. 任何建议欢迎。

Thanks to log4net LogicalThreadContext not working . 感谢log4net LogicalThreadContext无法正常工作

The following code will work across threads following upgrade to log4net version 1.12.13.0 升级到log4net版本1.12.13.0后,以下代码将在所有线程上工作

log4net.LogicalThreadContext.Properties["CallerIp"] = ip;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM