Contents
1.2.SCOPE
WCF is one of the latest technologies
of Microsoft that is used to build service-oriented applications. Based on the
concept of message-based communication, in which an HTTP request is represented
uniformly, WCF makes it possible to have a unified API irrespective of diverse
transport mechanisms. WCF is mostly used in Windows platform systems, so
logging error messages to windows event viewer will help the developer to
easily identify the errors when it occurs.
WCF-Windows Communication Foundation
<This section should list all the applicable and reference
documents>
Maintaining a log is one of the important aspects in
programming. If we are maintaining the logs correctly, then we can simply
identify if any errors are occurring in our program. Programmers are using many
techniques to Maintain Logs. Some of the most commonly used techniques in C# is
listed below
1) Creating a log file and maintaining the logs in that file.
2) Writes logs to Windows Event Log
3) log4net
Here we are looking for the second one Write Logs to Windows
Event Log.
Step 1) Enable Fault Contract in WCF Application
Step 2) Get the error details from WFC to Client using Fault
Contract
Step 3) Write error details log to Windows Event Log
We can go step by step process.
First we need to get the error details from WCF in our client
Application
See the below code which implement Fault Contract, using the
help of below code
We can pass error details to client from WCF.
Ø Create a sample WCF project
Ø Create file ICalculatorService.cs
Ø Create file Calculator.cs
Ø Create a sample WCF project
Ø Create file ICalculatorService.cs
Ø Create file Calculator.cs
Replace ICalculator.cs with following content.
[ServiceContract]
using System;
using System.Runtime.Serialization;
using System.ServiceModel;
namespace Calculator
{
[ServiceContract]
public interface ICalculatorService
{
[OperationContract]
[FaultContract(typeof(ErrorDetails))]
ResultSet
PerformCalculatorOperations(int firstInput,int secondInput,string operationType);
}
[DataContract]
public class ResultSet
{
[DataMember]
public int Result { get; set; }
[DataMember]
public string Staus { get; set; }
}
public class ErrorDetails
{
public string ErrorInfo { get; set; }
}
}
Replace Calculator.cs file with following content.
using System;
using System.ServiceModel;
namespace Calculator
{
public class CalculatorService : ICalculatorService
{
ResultSet result = new ResultSet();
ErrorDetails errorDetails = new ErrorDetails();
public ResultSet
PerformCalculatorOperations(int firstInput, int secondInput, string operationType)
{
try
{
switch (operationType)
{
case "Addition":
Addition(firstInput,
secondInput);
break;
case "Subtraction":
Subtraction(firstInput,
secondInput);
break;
case "Multiplication":
Multiplication(firstInput, secondInput);
break;
case "Division":
Division(firstInput,
secondInput);
break;
default:
result.Staus = "Error";
break;
}
}
catch (Exception ex)
{
errorDetails.ErrorInfo =
ex.ToString();
throw new FaultException<ErrorDetails>(errorDetails, ex.ToString());
}
return result;
}
public void Addition(int firstInput, int secondInput)
{
result.Result = firstInput +
secondInput;
result.Staus = "Sucess";
}
public void Subtraction(int firstInput, int secondInput)
{
result.Result = firstInput -
secondInput;
result.Staus = "Sucess";
}
public void Multiplication(int firstInput, int secondInput)
{
result.Result = firstInput *
secondInput;
result.Staus = "Sucess";
}
public void Division(int firstInput, int secondInput)
{
result.Result = firstInput /
secondInput;
result.Staus = "Sucess";
}
}
}
Ø Create A sample MVC Project for consuming WCF Service
Ø Ddd above wcf service reference in newly created MVC Project.
Ø Create a controller named CalculatorConsumer
Replace the content of CalculatorConsumerController with
following code.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.ServiceModel;
using System.Web;
using System.Web.Mvc;
namespace CalculatorConsumer.Controllers
{
public class CalculatorConsumerController : Controller
{
//
GET: CalculatorConsumer
public ActionResult Index()
{
Calculator.CalculatorServiceClient consumer = new Calculator.CalculatorServiceClient();
try
{
var consumerResult =
consumer.PerformCalculatorOperations(11, 0, "Division");
}
catch (FaultException<Calculator.ErrorDetails> errorDetails)
{
string ErrorDetails =
errorDetails.Detail.ErrorInfo;
// Create an EventLog instance and
assign its source.
EventLog eventLog = new EventLog();
eventLog.Source = "FromWCF";
// Create the source and log, if it
does not already exist.
if (!EventLog.SourceExists("WCF"))
{
EventLog.CreateEventSource("WCF", "WCFLog");
}
// Write an entry in the event
log.
eventLog.WriteEntry(ErrorDetails, EventLogEntryType.Warning, 1001);
}
return View();
}
}
}
Now you can check your Windows Event Log.
Control Panel->Administrative Tools->Event
Viewer->Windows Logs->Application
Then if you are clicking the Particular log then you can see
the details and general information like below.
No comments:
Post a Comment