Hasan Shouman
Hasan Shouman

Reputation: 2362

Checking if a WCF service is called

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,

Upvotes: 1

Views: 466

Answers (3)

oleksa
oleksa

Reputation: 4007

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

Upvotes: 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;
}

Upvotes: 1

Davide Piras
Davide Piras

Reputation: 44605

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.

Upvotes: 3

Related Questions