no configuration section <common/logging> found - asp.net-mvc-4

I'm using system.diagnostic to log all the errors to a log file
Web.Config:
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="MyListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="MyListenerLog.txt" />
<remove name="Default" />
</listeners>
</trace>
</system.diagnostics>
Code:
private static void AddToMyListner(string message)
{
try
{
System.Diagnostics.Trace.WriteLine("Text: " +message + "," + DateTime.UtcNow);
System.Diagnostics.Trace.Close();
}
catch (Exception ex)
{
throw ex;
}
}
In the Log file the first log i got was
no configuration section <common/logging> found - suppressing logging output
This log is printed only once i.e. only when I create a new log file. I'm not using Common.Logging so i was wondering what is causing this issue.

Common.Logging is commonly integrated with tracing. If you're getting this error and you're not deliberately referencing Common.Logging then you're probably referencing some external library that uses Common.Logging and subscribes to the trace by default. If there's no configuration in place (reasonable, since you're not intending to use Common.Logging) then this would happen when Common.Logging receives a notification that something was written to the trace and doesn't know what to do about it.

Related

REST WCF service returns XML response but not JSON response

I am new to WCF so I think this is pretty basic. I have a simple method that a single "order" object is returned. It works just fine when using the default XML, however, when I apply the
ResponseFormat = WebMessageFormat.Json
attribute, it fails to return JSON. The code successfully executes and hits the return line but then the method is immediately called again and then finally a third time before the browser returns an error stating the connection to localhost has been interrupted.
When I remove the ResponseFormat = WebMessageFormat.Json, the method is called and XML is returned just fine. Not sure I am missing for the JSON.
IProductSales.cs
namespace ProductsSalesService
{
[ServiceContract(Name = "ProductsSales")]
public interface IProductsSales
{
[OperationContract]
[WebGet(UriTemplate = "Orders/{orderID}", ResponseFormat = WebMessageFormat.Json)]
[Description("Returns the details of an order")]
SalesOrderHeader GetOrder(string orderID);
}
}
ProductSales
public SalesOrderHeader GetOrder(string orderID)
{
SalesOrderHeader header = null;
try
{
int id = Convert.ToInt32(orderID);
AdventureWorksEntities database = new AdventureWorksEntities();
header = (from order in database.SalesOrderHeaders
where order.SalesOrderID == id
select order).FirstOrDefault();
}
catch
{
throw new WebFaultException(HttpStatusCode.BadRequest);
}
return header;
}
I am working through an sample in a WCF book so they had me build a small console application to be the host, so this is the app.config file I have for the host client.
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="AdventureWorksEntities" connectionString="metadata=res://*/ProductsSalesModel.csdl|res://*/ProductsSalesModel.ssdl|res://*/ProductsSalesModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=BINGBONG;Initial Catalog=AdventureWorks;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup><system.serviceModel>
<services>
<service name="ProductsSalesService.ProductsSales">
<endpoint address="http://localhost:8000/Sales" binding="webHttpBinding"
bindingConfiguration="" name="ProductsSalesService.ProductsSales"
contract="ProductsSalesService.IProductsSales" />
</service>
</services>
</system.serviceModel>
</configuration>
Finally, this is just the host client code.
public class Program
{
static void Main(string[] args)
{
WebServiceHost host = new WebServiceHost(typeof(ProductsSalesService.ProductsSales));
host.Open();
Console.WriteLine("Service running");
Console.WriteLine("Press ENTER to stop the service");
Console.ReadLine();
host.Close();
}
}
So when I go to http://localhost:8000/Sales/Orders/43659 to pull up my order it hits three times and the page cancels in Chrome with the following error:
This webpage is not available The connection to localhost was
interrupted. Here are some suggestions: Reload this webpage later.
Check your Internet connection. Restart any router, modem, or other
network devices you may be using. Add Google Chrome as a permitted
program in your firewall's or antivirus software's settings. If it is
already a permitted program, try deleting it from the list of
permitted programs and adding it again. If you use a proxy server,
check your proxy settings or contact your network administrator to
make sure the proxy server is working. If you don't believe you should
be using a proxy server, adjust your proxy settings: Go to the wrench
menu > Settings > Show advanced settings... > Change proxy settings...
LAN Settings and deselect the "Use a proxy server for your LAN" checkbox. Error 101 (net::ERR_CONNECTION_RESET): The connection was
reset.
If I remove the WebMessageFormat.Json everything works fine!
Thanks for any assistance!
For starters try WCF tracing/logging to see if it sheds any light on things.
Put this in your server's config file (somewhere within the <configuration> element):-
<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Error" propagateActivity="true">
<listeners>
<add name="traceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData="C:\Temp\server.svclog"/>
</listeners>
</source>
<source name="System.ServiceModel.MessageLogging">
<listeners>
<add name="messages"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="C:\Temp\server_messages.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>
And put this inside the <system.serviceModel> element:-
<diagnostics>
<messageLogging
logEntireMessage="true"
logMalformedMessages="false"
logMessagesAtServiceLevel="true"
logMessagesAtTransportLevel="false"
maxMessagesToLog="3000"
maxSizeOfMessageToLog="2000"/>
</diagnostics>
Try hitting your service again and examine the .svclog files that this (hopefully) generates for clues. The files will open in a "Service Trace Viewer" tool - if not it can be downloaded from MS (part of the Win SDK I think).
Although my fault is actually unrelated, this is the first article I found when looking into my problem which is that my service was failing and I was seeing an error connection has been interrupted.
My fault was to do with the fact that the class I was outputting from my WebGet method had properties that had DataContract attributes but I had not added a Set accessor to each one (because I considered them to be output-only I didn't see the point).
Adding the tracing into my configuration file quickly revealed that the fault was that there were no Set accessors, so I added private set accessors to each DataContract property and all is now working as expected.
I have added this here in case anyone else follows the same search path and has the same issue.
This line of code will construct Service Host without taking configuration into account so you will have Service but it will listen different URL.
WebServiceHost host = new WebServiceHost(typeof(ProductsSalesService.ProductsSales));
Add base address new WebServiceHost plus code below:
WebChannelFactory<ProductsSalesService.IProductsSales> cf =
new WebChannelFactory<ProductsSalesService.IProductsSales>("ProductsSalesService.ProductsSales");
ProductsSalesService.IProductsSales channel = cf.CreateChannel();
See full code here - http://msdn.microsoft.com/en-us/library/bb919583

WCF tracing & message logging - trace level warning

Assume I have a config file which looks like this:
...
<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Warning,ActivityTracing" propagateActivity="true">
<listeners>
<add name="ServiceModelTraceListener" />
</listeners>
</source>
<source name="System.ServiceModel.MessageLogging">
<listeners>
<add name="ServiceModelTraceListener" />
</listeners>
</source>
</sources>
<sharedListeners>
<add initializeData="LogServer.svclog" type="System.Diagnostics.XmlWriterTraceListener" name="ServiceModelTraceListener" />
</sharedListeners>
<trace autoflush="true" />
</system.diagnostics>
When using this config file every activity the caller performs against the service and each corresponding message that's sent to the service will be logged in the svclog file. Everything fine so far.
If I modify the 3rd line from the above listing to <source name="System.ServiceModel" switchValue="Warning" propagateActivity="true"> (the ActivityTracing is removed) then only those activities are logged that are at least labeled level warning. But it's still every message logged...
So is there a way to only log those message that correspond to those activities that are at least warnings? Those messages that succeeded aren't very interesting in that moment, but those messages that belong to the unsuccessful activities are!
Edit
To filter messages beyond the options below you may want to look into writing your own TraceSource.
Below is one I am using for a project. You could easily customize it to filter out the messages you want or perhaps hide activity if it is not in DEBUG, etc.
class DB : TraceSource
{
public DB(string name) : base(name)
{
}
public DB(string name, SourceLevels sourceLevels) : base (name, sourceLevels)
{
}
public void Log(object value)
{
WriteLine(value);
}
public void Error(object value)
{
WriteLine(value, TraceEventType.Error);
}
public void Error(RecordingResponseData errorResponse)
{
string errorMessage = "[Error] Code: "+errorResponse.ErrorCode +" Message: "+errorResponse.ErrorMessage;
WriteLine(errorMessage, TraceEventType.Error);
}
public void Warn(object value)
{
WriteLine(value, TraceEventType.Warning);
}
public void WriteLine(object value, TraceEventType type = TraceEventType.Information)
{
TraceEvent(type, 0, value.ToString());
}
}
Original
Your options are:
Critical
Error
Warning
Information
ActivityTracing
Verbose
All
Or a combination there of. If you have it set to Warning but are still getting too many messages then you may want to try Error or Critical.
ref: https://msdn.microsoft.com/en-us/library/ms733025%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
There's a switchValue available for the System.ServiceModel.MessageLogging trace switch as well. Just add that attribe to that source element and set it to Warning also and you will only see messages logged that are related to warnings.
Get rid of System.ServiceModel.MessageLogging source to get rid of logging messages to resolve "still every message logged".

SOAP message is empty when catching MessageLoggingTraceRecords with CustomTraceListener

I want to write CustomTraceListener which writes all data to SQL Server DB.
Here's the stub for it:
public class SqlTraceListener : TraceListener
{
public SqlTraceListener()
: base()
{ }
public SqlTraceListener(String name)
: base(name)
{ }
protected override string[] GetSupportedAttributes()
{
List<string> attributes = new List<string>();
attributes.Add("connectionString");
attributes.Add("actionFilter");
attributes.Add("hostFilter");
return base.GetSupportedAttributes();
}
public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id, object data)
{ }//Other empty methods...
}
In overridden method TraceData I want to catch SOAP messages sent to my WCF service. But when I check what is in "data" parameter I get this: (sorry for posting xml as pictures - it seems SO editor doesn't allow some xml keywords in posts):
But according to standard XmlWriterTraceListener I should get this:
How to configure TraceListener not to eliminate SOAP messages?
My config is here:
<system.diagnostics>
<sources>
<source name="System.ServiceModel.MessageLogging">
<listeners>
<add name="xml"/>
<add name="sql"/>
</listeners>
</source>
</sources>
<sharedListeners>
<add initializeData="C:\logs\StockPriceService.svclog" type="System.Diagnostics.XmlWriterTraceListener" name="xml"/>
<add type="SqlTraceListener.SqlTraceListener, SqlTraceListener" name="sql"/>
</sharedListeners>
<trace autoflush="true"/>
Is there any reason you're not using the out of the box database trace listener? See: Enterprise Library Database Trace Listener?.

WCF slow ServiceHost.Open() call

This is a similar question as this one:
Win32Exception # ServiceHost.Open() for WCF service.
I have a machine that is very slow on the ServiceHost.Open call below. It consistently takes 7 seconds or so to open the service, every time. This machine is my home box, and it is not part of a domain.
I can run the same code on another box (my work box) which is part of a domain and the service host opens in about 3-4 seconds on the first call, but if I run the program again the service host opens in about 1 second or less.
I have worked with MS support on this, and we generated trace logs and the part it's hanging in is where is goes out and tries to connect to a domain, even on the machine that isn't part of a domain. And it gets the "The specified domain either does not exist or could not be contacted." exception in the trace log, and that's where all the time is getting eaten up.
But what's really weird is that even on my work machine, if I'm not connected to a domain (like if I'm not on my work network and just running from home) I still don't get the delay.
I rebuilt my machine using Windows 7 64-bit, and the same behavior occurs (was running XP SP3, which I restored when Windows 7 didn't seem to fix the problem).
I just wondered if anyone had any ideas of what could cause this. By the way, if I disable "Client for microsoft networks", the time is like 4 seconds to open the ServiceHost, but that's still not as fast as this machine used to be able to open the service. Somehow, it thinks it's supposed to be part of a domain or something.
static void RunServiceWithinEXE()
{
Uri baseAddress = new Uri("http://localhost:11111/Demo");
ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress);
try
{
// Add a service endpoint
serviceHost.AddServiceEndpoint(
typeof(ICalculator),
new WSHttpBinding(),
"CalculatorService");
// Enable metadata exchange
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
serviceHost.Description.Behaviors.Add(smb);
serviceHost.Opening += new EventHandler(serviceHost_Opening);
serviceHost.Opened += new EventHandler(serviceHost_Opened);
serviceHost.Open();
Console.WriteLine("The service is ready.");
// Close the ServiceHostBase to shutdown the service.
serviceHost.Close();
}
catch (CommunicationException ce)
{
Console.WriteLine("An exception occured: {0}", ce.Message);
serviceHost.Abort();
}
}
static void serviceHost_Opened(object sender, EventArgs e)
{
TimeSpan timeToOpen = DateTime.Now - shOpening;
Console.WriteLine("Time To Open: :" + timeToOpen.Seconds);
}
static void serviceHost_Opening(object sender, EventArgs e)
{
shOpening = DateTime.Now;
}
Here is my app.config, but I don't have any special security configuration settings for the service in there, only some diagnostic settings to enable the WCF trace.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<diagnostics>
<messageLogging maxMessagesToLog="30000"
logEntireMessage="true"
logMessagesAtServiceLevel="false"
logMalformedMessages="true"
logMessagesAtTransportLevel="true">
<filters>
<clear/>
</filters>
</messageLogging>
</diagnostics>
</system.serviceModel>
<system.diagnostics>
<sources>
<source name="System.ServiceModel" switchValue="Warning, ActivityTracing" propagateActivity="true" >
<listeners>
<add name="xml" />
</listeners>
</source>
<source name="System.ServiceModel.MessageLogging" switchValue="Warning">
<listeners>
<add name="xml" />
</listeners>
</source>
</sources>
<sharedListeners>
<add name="xml" type="System.Diagnostics.XmlWriterTraceListener" initializeData="C:\Temp\Server.svclog" />
</sharedListeners>
<trace autoflush="true" indentsize="4">
<listeners>
<remove name="Default" />
<add name="ScottsConsoleListener" type="System.Diagnostics.ConsoleTraceListener" />
<add name="ScottsTextListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\Temp\DebugLog.txt" />
</listeners>
</trace>
</system.diagnostics>
</configuration>
Note also that my service definition has SessionMode required (see below). If I take the SessionMode requirement out, I don't get the delays.
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace Microsoft.ServiceModel.Samples
{
// Define a service contract.
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples", SessionMode = SessionMode.Required)]
public interface ICalculator
{
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Subtract(double n1, double n2);
[OperationContract]
double Multiply(double n1, double n2);
[OperationContract]
double Divide(double n1, double n2);
[OperationContract]
string PrintName(string firstName, string lastName);
[OperationContract]
Point MakePoint(double x, double y);
}
}
Okay, my suspicion is the fact you're using a binding (WsHttpBinding) which defaults to authenticating its callers using Windows credentials unless you specifically tell it not to.
In your service hosting code, you're just instantiating a default WsHttpBinding instance - no settings in config or code to change the default security behavior, in that case.
Just for testing purposes: try to change your service hosting code to:
// Add a service endpoint
serviceHost.AddServiceEndpoint(
typeof(ICalculator),
new WSHttpBinding(SecurityMode.None), // pass in SecurityMode.None
"CalculatorService");
This will effectively cancel out all security settings, e.g. the ServiceHost should no longer even attempt to find the Windows domain to authenticate callers against.
Does that change any of your observations?
It turns out that if I disable netbios on my network connections, I don't get the delays on the ServiceHost.Open calls. I am not sure why, but this seemed to fix the problem.
What's weird is that when I did a clean install of XP, I didn't have the delays even with the Netbios enabled, so I have no idea why it happens with my existing installation (I did the clean install on this same machine, and then restored my backup from an image to run these tests).
Also try stopping the Remote Access Auto Connection Manager service. For me, the issue came down to Dns.GetHostEntry(String.Empty) and this post http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/493d1b65-9ace-41de-b269-f178d27a8a1b
It seems I had the same Problem. Long Duration of Open call caused by an exception. I googled for configuration of security Settings for Wcf Service. Found the following solution which worked for me:
Under <wsHttpBinding> element in the Web.config file place the following entry:
<security mode="None" />
The Service Reference in the Client must be updated!

WCF tracing in code does not follow MessageLogging settings

I need to use WCF tracing in my application but it needs to be controlled from code as much as possible.
IT was suggested that I install the following sections in my app.config file:
<configuration>
<system.serviceModel>
<diagnostics>
<messageLogging
maxMessagesToLog="100"
logEntireMessage="true"
logMessagesAtServiceLevel="true"
logMalformedMessages="true"
logMessagesAtTransportLevel="true">
</messageLogging>
</diagnostics>
</system.serviceModel>
<system.diagnostics>
<sources>
<source name="System.ServiceModel" >
<listeners>
<add type="System.Diagnostics.DefaultTraceListener" name="dummy"/>
</listeners>
</source>
</sources>
</system.diagnostics>
</configuration>
Then the following code could be used to get the trace running as needed:
BindingFlags privateMember = BindingFlags.NonPublic | BindingFlags.Instance;
BindingFlags privateStaticMember = privateMember | BindingFlags.Static;
Type type = Type.GetType("System.ServiceModel.DiagnosticUtility, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
MethodInfo[] mi = type.GetMethods(privateStaticMember);
// invoke InitializeTracing
object diagnosticTrace = mi.FirstOrDefault(e => e.Name == "InitializeTracing").Invoke(null, null);
if (diagnosticTrace != null)
{
// get TraceSource
Type type2 = Type.GetType("System.ServiceModel.Diagnostics.DiagnosticTrace, SMDiagnostics, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
PropertyInfo pi = type2.GetProperty("TraceSource", privateMember);
TraceSource traceSource = pi.GetValue(diagnosticTrace, null) as TraceSource;
// clear all listeners in the trace source
traceSource.Listeners.Clear();
// add listener to trace source
XmlWriterTraceListener listener = new XmlWriterTraceListener("mylogfile".svclog");
listener.TraceOutputOptions = TraceOptions.Timestamp | TraceOptions.Callstack;
traceSource.Attributes["propagateActivity"] = "true";
traceSource.Switch.ShouldTrace(TraceEventType.Verbose | TraceEventType.Start);
traceSource.Listeners.Add(listener);
// enable tracing
type.GetProperty("Level", privateStaticMember).SetValue(null, SourceLevels.All, null);
Trace.AutoFlush = true;
This works fine up to a point, the main problem being that the messagelogging settings in the system.servicemodel section of the app.config file are being ignored.
Is there anything that can be done to solve this problem?
I can't comment on all of your code because I have not used System.Diagnostics in this way before (programmatically configuring the WCF communication tracing), but if your intent on this line:
traceSource.Switch.ShouldTrace(TraceEventType.Verbose | TraceEventType.Start);
Is to set the level of tracing that you want, I think that you should be using the Switch.Level property instead. ShouldTrace is for asking if a given TraceSource would trace, given the input flags.
traceSource.Switch.Level = SourceLevels.Verbose | SourceLevels.ActivityTracing;
Note that according to this link, it is possible to configure apparently reasonable settings and yet the activity id might not be propogated correctly. Read it carefully. It may or not apply to your situation.
You need to enable MessageLogging by defining a trace source as indicated in this MSDN Library page. So, you need this extra bit in your app.config in the sources section:
<source name="System.ServiceModel.MessageLogging">
<listeners>
<add type="System.Diagnostics.DefaultTraceListener" name="dummy"/>
<remove name="Default" />
</listeners> </source>
The message logging settings don't apply to the System.ServiceModel trace source.