1

I have built a WCF service using C# that is running on Windows server 2016.

I need to know when this WCF was called from the client, and who called it (for example the IP)

Is there a way to do this? I have tried to check the event viewer, and the IIS, but did not get to know how. Thanks,

Hasan Shouman
  • 2,162
  • 1
  • 20
  • 26
  • IIS log files should give you initial data on that, https://support.microsoft.com/en-ca/help/943891/the-http-status-code-in-iis-7-0-iis-7-5-and-iis-8-0 If you want more, the answers below provide further information. – Lex Li Jan 16 '20 at 15:13

3 Answers3

3

You really should use some kind of logging framework ( Log4Net, NLog, MS enterprise library logger... ),

that will allow you to log into text file, email, event log or database, and you will then be able to first of all document any error/exception thrown from your code so that you can investigate and resolve bugs, plus you can then also include information / verbose level log entries to capture, as you say, caller IP and timestamp as well as calling parameters if you like and need to do so.

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
  • 1
    See also [IServiceBehavior Interface](https://learn.microsoft.com/en-us/dotnet/api/system.servicemodel.description.iservicebehavior). If you combine the logging framework with this, you will have a WCF extension that will allow you to log access to any WCF service without modifying the original service. – Pepelui360 Jan 16 '20 at 11:26
1

Logging every request best thing you can do. If you want to know how to get client ip in WCF, method below will work for you. Then you could log that ip, request time etc.

public string GetClientIp()
{
  OperationContext operationContext = OperationContext.Current;
  MessageProperties messageProps = operationContext.IncomingMessageProperties;
  RemoteEndpointMessageProperty endpointProps = (RemoteEndpointMessageProperty)messageProps[RemoteEndpointMessageProperty.Name];

  return endpointProps.Address;
}
1

you can try existing Log WCF Service Calls with Parameter information logging with system.diagnostics configuration

or create custom implementation for IOperationInvoker like here Log WCF Service Calls with Parameter information

oleksa
  • 3,688
  • 1
  • 29
  • 54