简体   繁体   English

考虑使用WCF进行日志记录服务...请告知

[英]Considering Using WCF for a Logging Service… Please Advise

I am considering the architecture for an enterprise logging service. 我正在考虑企业日志服务的架构。 Its job would be to receive and store log messages and then allow access to those log messages to users. 它的工作是接收和存储日志消息,然后允许用户访问这些日志消息。 Instead of building the logging functionality into our one existing Windows service that would use it for now, we need to separate it so that other services could use it in the near future. 我们需要将它分开,以便其他服务可以在不久的将来使用它,而不是将日志记录功能构建到我们现在使用它的现有Windows服务中。 I like the fact that our various services could log their messages over net.tcp and then I could build a RESTful interface for delivering specific log messages to browsers or whatever. 我喜欢这样的事实:我们的各种服务可以通过net.tcp记录他们的消息,然后我可以构建一个RESTful接口,用于向浏览器或其他任何东西提供特定的日志消息。

Could anyone speak to the wisdom or lack of the following choices: 谁能说出智慧或缺乏以下选择:

  1. Use WCF for the logging service 使用WCF进行日志记录服务
  2. Use net.tcp for the transport 使用net.tcp进行传输
  3. Host the service in a Windows Service project (using ServiceHost) 在Windows服务项目中托管服务(使用ServiceHost)

Also, how might I design it such that it takes advantage of some rather beefy servers that are going to be hosting it? 另外,我如何设计它,以便利用一些相当强大的服务器来托管它? Is it possible to open up multiple connections (or is that done automatically) or implement some automatic multi-threading? 是否可以打开多个连接(或自动完成)或实现一些自动多线程?

The one service we currently have that would be utilizing this logging service is quite verbose and would be sending log messages very frequently (~40-100k/day). 我们目前使用的一种服务就是利用这种日志记录服务,这种服务非常冗长,并且会非常频繁地发送日志消息(~40-100k /天)。 I have not yet built up a prototype and done any benchmarking and I know I'm not giving you enough details to make a definitive decision, but I'm just looking for some direction and considerations at this point. 我还没有建立原型并进行任何基准测试,我知道我没有给你足够的细节来做出明确的决定,但我只是在寻找一些方向和考虑因素。 Thanks. 谢谢。

There are other alternatives to creating one more service just for logging. 还有其他替代方法可以仅为日志记录创建一个服务。 You could build the logging as an aspect and attach/detach this aspect (aka inject) as and when required by any ServiceContract or OperationContract . 您可以将日志记录构建为方面,并在任何ServiceContractOperationContract需要时附加/分离此方面(也称为注入)。 This way you decouple logging but it avoids the overhead of calling one more service on every call. 这样您就可以解除日志记录,但它避免了在每次调用时再调用一个服务的开销。 Once you create these aspects, compile them away in separate binary and use them as and when needed in all of your future services, enabling and disabling specific logging scenarios are more maintainable IMO compared to having a dedicated service for just logging. 一旦创建了这些方面,就可以在单独的二进制文件中对它们进行编译,并在需要时在所有未来的服务中使用它们,与拥有专门的日志记录服务相比,启用和禁用特定日志记录方案的IMO更易于维护。

Have a look at following two posts and they provide simplistic approach of doing this, you'd have to fill in the flesh as you want for your project. 看看以下两篇帖子,他们提供了简单的方法,你必须按照你想要的项目填写你的肉。

Important MSDN documentation you'd want to look at. 您想要查看的重要MSDN文档。

Edit - Sample Code 编辑 - 示例代码

With below code you add [OperationLogging] above any of your operation contract, and you can intercept calls to this operation contract in LoggingInspector.BeforeCall . 使用以下代码,您可以在任何操作合同上方添加[OperationLogging] ,并且可以在LoggingInspector.BeforeCall拦截对此操作合同的LoggingInspector.BeforeCall

Use [ServiceLogging] on any service contract and all the operations defined in that service calls could be intercepted and logged. 在任何服务合同上使用[ServiceLogging] ,可以拦截和记录该服务调用中定义的所有操作。

Set your_app_config_key to anything other than TRUE these additional behaviors are not added to your service pipeline. your_app_config_key设置为TRUE以外的任何值,这些附加行为不会添加到您的服务管道中。 That is very cool as none of this code is executed based on this key in config. 这非常酷,因为在配置中没有基于此键执行此代码。

public class LoggingInspector : IParameterInspector
{
    private string service;
    public LoggingInspector(string serviceName){ service = serviceName;}
    public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState){}
    public object BeforeCall(string operationName, object[] inputs)
    {
      // your logging logic
    }
}

 //Operation Logging attribute - applied to operationcontracts.
 [AttributeUsage(AttributeTargets.Method)]
 public class OperationLoggingAttribute : Attribute, IOperationBehavior
 {
    public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters){}
    public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation){}
    public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
    {
        if (ConfigurationManager.AppSettings["your_app_config_key"] == "TRUE")
            dispatchOperation.ParameterInspectors.Add(new LoggingInspector(dispatchOperation.Parent.Type.Name));
    }
    public void Validate(OperationDescription operationDescription){}
 }

 //Service Loggign attribute - applied to Service contract
[AttributeUsage(AttributeTargets.Class)]
public class ServiceLoggingAttribute : Attribute, IServiceBehavior
{
    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters){}
    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        if (ConfigurationManager.AppSettings["your_app_config_key"] == "TRUE")
            foreach (ServiceEndpoint endpoint in serviceDescription.Endpoints)
                foreach (OperationDescription operation in endpoint.Contract.Operations)
                    operation.Behaviors.Add(new OperationLoggingAttribute());

    }
    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase){}
}

In principal I think a single audit service makes sense, provided it falls within the bounded context of your application(s). 原则上我认为单个审计服务是有意义的,只要它属于您的应用程序的有限上下文。 IDesign have a sample implementation of a ES Logbook here (look for the "The Enterprise Services Logbook"). IDesign 在此处具有ES日志的示例实现(查找“企业服务日志”)。 You could do some initial testing to see if it can handle the loads you are expecting. 您可以进行一些初步测试,看看它是否能够处理您期望的负载。 If you are worried about performance I would consider message queuing over tcp (the sample logging application also supports this). 如果您担心性能,我会考虑通过tcp进行消息排队(示例日志记录应用程序也支持此功能)。 As for hosting, the service needs to be always running, so Windows Service makes sense. 至于托管,服务需要始终运行,因此Windows服务是有意义的。 If you want to use IIS then I would suggest using Windows Server AppFabric and enable the AutoStart feature of the application. 如果您想使用IIS,那么我建议使用Windows Server AppFabric并启用应用程序的AutoStart功能。

HTH. HTH。

Reading the question i have some thoughts to share. 阅读这个问题我有一些想法可以分享。 Logging itself is not a terribly complex activity and using WCF to create a enterprise logging framework would be fine. 记录本身并不是一个非常复杂的活动,使用WCF创建企业日志框架会很好。 But data logged just for logging is of no use. 但是仅记录日志记录的数据是没有用的。 This data then needs to be consumed by some process\\app and hence provide some value addition. 然后,某些进程\\ app需要使用此数据,从而提供一些附加值。 Therefore the more important aspect of logging are 因此,日志记录的更重要方面是

  • What data is being logged. 记录了哪些数据。
  • How data logged is being consumed\\utilized 如何消耗或利用数据记录

So my advice would be to spend more time thinking about what needs to be logged and what value this data adds. 所以我的建议是花更多时间考虑需要记录的内容以及这些数据增加的价值。

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

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