我有一套web服務,我想添加一個跟蹤圖層。 我不想修改每個Web服務,因爲我有很多。 我想寫入每一個輸入到Web服務的日誌:Web服務和參數的名稱。webservices跟蹤/日誌
這樣做的最佳方法是什麼?
P.S.我正在使用asp.net和C#。
編輯: 我只想包裹Web服務作爲每一個都會有開頭日誌(..)。
我有一套web服務,我想添加一個跟蹤圖層。 我不想修改每個Web服務,因爲我有很多。 我想寫入每一個輸入到Web服務的日誌:Web服務和參數的名稱。webservices跟蹤/日誌
這樣做的最佳方法是什麼?
P.S.我正在使用asp.net和C#。
編輯: 我只想包裹Web服務作爲每一個都會有開頭日誌(..)。
實現此目的的常見方法是注入SOAP擴展。從那裏你可以攔截原始SOAP中的每個請求/響應數據包。該示例展示瞭如何實現它,並解釋了它如何工作以及如何配置它。
樣品:
http://msdn.microsoft.com/en-us/library/system.web.services.protocols.soapextension.aspx
說明:
http://msdn.microsoft.com/en-us/library/esw638yk(vs.71).aspx
配置:
http://msdn.microsoft.com/en-us/library/b5e8e7kk(v=vs.71).aspx
<configuration>
<system.web>
<webServices>
<soapExtensionTypes>
<add type="{Type name}, {Assembly}" priority="1" group="0" />
</soapExtensionTypes>
</webServices>
</system.web>
</configuration>
我保持Open source web services framework,可以讓你簡單地通過使所有Web服務從基類繼承,做你自己的日誌實現這一目標。
這裏是一個example of a base-class,我保持一個分佈式滾動日誌所有異常的redis - 非常快的NoSQL數據存儲庫:
public object Execute(TRequest request)
{
try
{
//Run the request in a managed scope serializing all
return Run(request);
}
catch (Exception ex)
{
return HandleException(request, ex);
}
}
protected object HandleException(TRequest request, Exception ex)
{
var responseStatus = ResponseStatusTranslator.Instance.Parse(ex);
if (EndpointHost.UserConfig.DebugMode)
{
// View stack trace in tests and on the client
responseStatus.StackTrace = GetRequestErrorBody() + ex;
}
Log.Error("ServiceBase<TRequest>::Service Exception", ex);
//If Redis is configured, maintain rolling service error logs in Redis (an in-memory datastore)
var redisManager = TryResolve<IRedisClientsManager>();
if (redisManager != null)
{
try
{
//Get a thread-safe redis client from the client manager pool
using (var client = redisManager.GetClient())
{
//Get a client with a native interface for storing 'ResponseStatus' objects
var redis = client.GetTypedClient<ResponseStatus>();
//Store the errors in predictable Redis-named lists i.e.
//'urn:ServiceErrors:{ServiceName}' and 'urn:ServiceErrors:All'
var redisSeriviceErrorList = redis.Lists[UrnId.Create(UrnServiceErrorType, ServiceName)];
var redisCombinedErrorList = redis.Lists[UrnId.Create(UrnServiceErrorType, CombinedServiceLogId)];
//Append the error at the start of the service-specific and combined error logs.
redisSeriviceErrorList.Prepend(responseStatus);
redisCombinedErrorList.Prepend(responseStatus);
//Clip old error logs from the managed logs
const int rollingErrorCount = 1000;
redisSeriviceErrorList.Trim(0, rollingErrorCount);
redisCombinedErrorList.Trim(0, rollingErrorCount);
}
}
catch (Exception suppressRedisException)
{
Log.Error("Could not append exception to redis service error logs", suppressRedisException);
}
}
var responseDto = CreateResponseDto(request, responseStatus);
if (responseDto == null)
{
throw ex;
}
return new HttpResult(responseDto, null, HttpStatusCode.InternalServerError);
}
否則,對於普通的ASP.NET Web服務框架,我想看看全球.asax事件,特別是每當新請求進入時觸發的'Application_BeginRequest'事件。
@mythz:請透露您與ServiceStack的連接。儘管這是一個開源項目,但像這樣的建議仍然等同於垃圾郵件。這不是我第一次見到你推薦ServiceStack。除其他問題外,您還沒有展示如何使用它來解決這個問題。 – 2011-01-19 00:43:15
我已經提出瞭如何在他使用其他Web服務框架的情況下很容易實現的建議。原來的問題從未指定過它是什麼。我最初的回答也提供了Global.asax事件的位置,可以讓他實現他的目標。你爲什麼不自己提供一個更好的答案?我個人認爲,您對特定Web服務技術/框架的規範是您行爲的主要動機。 – mythz 2011-01-19 01:02:45
@mythz:它看起來像他指定ASP.NET Web服務。那和我以前從未聽說過服務堆的事實可能與我有禮貌地問你,要透露。謝謝你這樣做,我希望你每次推薦這個框架時都會這樣做。你也應該建議實際展示你的框架如何解決這個問題。 – 2011-01-19 01:13:30
如果編程語言不重要,可以將Apache Synapse作爲代理服務器放在您的服務之前。您的客戶會將請求發送給Synapse,Synapse會將請求委託給您的原始服務。可以將代理配置爲對其中的請求執行某些操作,例如日誌記錄。
請參閱有關詳細信息,下面的鏈接:
http://synapse.apache.org/Synapse_Configuration_Language.html#proxy,
http://synapse.apache.org/Synapse_Configuration_Language.html#send,
http://synapse.apache.org/Synapse_Configuration_Language.html#log
以下實例的組合可以爲你工作:
http://synapse.apache.org/Synapse_Samples.html#Sample0
http://synapse.apache.org/Synapse_Samples.html#ProxyServices
例如: -
<definitions xmlns="http://ws.apache.org/ns/synapse"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ws.apache.org/ns/synapse http://synapse.apache.org/ns/2010/04/configuration/synapse_config.xsd">
<proxy name="StockQuoteProxy">
<target>
<endpoint>
<address uri="http://localhost:9000/services/SimpleStockQuoteService"/>
</endpoint>
<outSequence>
<!-- log all attributes of messages passing through -->
<log level="full"/>
<!-- Send the message to implicit destination -->
<send/>
</outSequence>
</target>
<publishWSDL uri="file:repository/conf/sample/resources/proxy/sample_proxy_1.wsdl"/>
</proxy>
如何writing your own HttpModule?這將否定觸摸現有Web服務代碼的需要。您只需將模塊添加到每個web.config文件。
將全局應用程序類Global.asax文件添加到您的項目並將日誌記錄邏輯添加到Application_BeginRequest()方法。 發件人對象將包含HTTP請求和參數。您可以篩選.asmx請求並記錄這些請求。
protected void Application_BeginRequest(object sender, EventArgs e)
{
}
編輯 -
給PostSharp一個嘗試。這是獲得此功能的最簡單方法。對於後代,我會在下面留下我的帖子,但忽略它並使用PostSharp。
如果您的網絡服務是WCF,那麼你應該檢查出http://msdn.microsoft.com/en-us/magazine/cc163302.aspx。
在每一步中,他們都提供了可插入的擴展點。您可以使用這些擴展點來實現各種自定義行爲,包括消息或參數驗證,消息日誌記錄,消息轉換。
毫無疑問,這是WCF服務的方式。否則,如果他們只是Web服務,那麼你可以使用Unity框架和連接和Interceptor來做同樣的事情。
我不知道如果這是你在找什麼,只是將它添加到您WCF配置文件後,「」
這將產生非常廣泛的日誌記錄,你將能夠使用微軟閱讀服務跟蹤查看器
<system.diagnostics>
<sources>
<source name="System.ServiceModel.MessageLogging" switchValue="Warning, ActivityTracing">
<listeners>
<add type="System.Diagnostics.DefaultTraceListener" name="Default">
<filter type="" />
</add>
<add name="ServiceModelMessageLoggingListener">
<filter type="" />
</add>
</listeners>
</source>
<source name="System.ServiceModel" switchValue="Warning, ActivityTracing"
propagateActivity="true">
<listeners>
<add type="System.Diagnostics.DefaultTraceListener" name="Default">
<filter type="" />
</add>
<add name="ServiceModelTraceListener">
<filter type="" />
</add>
</listeners>
</source>
</sources>
<sharedListeners>
<add initializeData="C:\ServiceLog.svclog"
type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp">
<filter type="" />
</add>
<add initializeData="C:\Tracelog.svclog"
type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
name="ServiceModelTraceListener" traceOutputOptions="Timestamp">
<filter type="" />
</add>
</sharedListeners>
</system.diagnostics>
「我不想修改每個Web服務,因爲我有很多」 - 估計你可以使用AOP,否則我認爲你將不得不這樣做。 ... – 2011-01-08 03:11:12
您可以使用服務器訪問日誌 – 2011-01-08 03:14:07
請參閱SoapExtension類上的MSDN文檔。它有一個日誌記錄的例子。 – 2011-01-08 03:54:25