Trace / Debug app.config file reads / access operations - c#

Is there a mechanism (or config file setting) that will trace read operations against a .NET config file?
Background: I'm working on a SOAP test client application and there's one point where it's complaining about not finding the endpoint element and contract details in the configuration file. There are 9 services, 8 are working fine. The naming of the problem service is complicated with spelling errors (service name, wsdl port name...) I'm starting to suspect that parts of the .NET framework use convention rather than direct configuration to locating parts of the binding configuration. I think I have the name mutated correctly in the various places but when making the connection it still errors.
After all that, what would help is if I could get a trace output as the program (framework) reads through the configuration file. It would be easy at that point to see what specifically is miss-spelled or where it is my mistake.
It's almost impossible to search for this topic, all the hits along the lines of "debugging config file" return links for adding logging to a config file or configuration management of an app build.

Related

Writing to event log by using data from config file

I am doing some practice tests for Microsoft's "Programming in C#" exam 70-483 and one of the questions has me in a stump and I don't know enough about tracing to test it properly.
The question is this:
You are developing an application that uses a .config file.
The relevant portion of the .config file is shown as follows:
<system.diagnostics>
<trace autoflush="false" indentsize="0">
<listeners>
<add name="appListener"
type="System.Diagnostics.EventLogTraceListener"
initializeData="TraceListenerLog" />
</listeners>
</trace>
</system.diagnostics>
You need to ensure that diagnostic data for the application writes to the event log by using the configuration specified in the .config file.
What should you include in the application code?
A. EventLog log = new EventLog();
<br/>log.WriteEntry("Trace data...");
B. Debug.WriteLine("Trace data...");
C. Console.SetOut(new StreamWriter("System.Diagnostics.EventLogTraceListener")); <br/>Console.WriteLine("Trace data...");
D. Trace.WriteLine("Trace data...");
I'm thinking "C" because it's the only option that has something to do with the .config file, but it says the correct answer is "D" for some reason. Mind you these questions have been wrong before about completely simple and obvious stuff, so...
Can you please explain which is the correct answer and why?
The solution is to read about tracing instead of trying to just find the correct answer.
C is the absolute worst - not only does it have nothing to do with System.Diagnostics or the EventLog, it will try to create a file named System.Diagnostics.EventLogTraceListener.
The only options that actually use System.Diagnostics are B and D.
The <system.diagnostics><trace> section in app.config configures the System.Diagnostics classes, including sources, listeners, switches that activate or deactive specific sources in the application etc. It's no different really than the configuration files used for any other log library like log4net, Serilog or NLog.
Debug.WriteLine("Trace data..."); will write the output to all the configured trace listeners, only in a debug build.
Trace.WriteLine("Trace data..."); will write to all configured trace listeners both in debug and release builds.
Option A is wrong too. It does involve the event log, but won't work because the Source isn't set. EventLog.WriteEntry doesn't send any diagnostic information either, it just writes an Information message to the Event Log.
The System.Diagnostics listener on the other hand will receive diagnostic events from the myriad of trace sources in .NET, including the application's own, filter it, and write it to its target.
Do you have a problem with networking for example? You can switch the System.Net trace listeners on and collect trace messages from the HTTP level all the way down to sockets, SSL, TCP and even packet operations.
The correct answer is the "D", What that configuration file is doing.. is adding an instance to the trace.Listeners. So whenever you call Trace.WriteLine one of the outputs will be to that listener you set up in the config file.
"C" would work too, but it does say that you need to ensure that you use the config file, and what you are doing in C has nothing to do with the config file.

Hide/Encrypt WCF Client End Points in app.config

I have made a WPF application, and i am using multiple WCF services(deployed on remote server) in it. Currently all client configurations(end points) are given in app.config. If i open App.exe file (File Type: XML Configuration File) in notepad then i can see all configurations (end points) there.
What i want is that i want to hide that configurations so that end user can't see my configuration details (especially Address attribute).
Does Somebody know how to secure/hide my WCF end point? Either i need to use some tool to encrypt app.config file or i need to write client end points in code (if yes then how) ?
Thanks in Advance :)
If you're worried about divulging your endpoint address, you're doing security wrong. It sounds like what you need is--at minimum--to authenticate your WCF service properly.
I recommend starting with the WCF Security documentation.

Refreshing config file values

I have this strange problem having hard time correcting it. Whenever I update options in my config files it wont detect the changes. I will keep getting exception error saying that option not detected, even i refresh the VS-2012, re start VS02012 and IIS, refesh browsers. It takes long time before it will detect the changes and I can use them. Error I get is
System.Exception: unable to vend object, interface [abc.IExec] reference [option.changeEmployees] ---> System.Exception: option set not found [api_changeEmployees]
While this config file has those options values is saved and updated. What is the fix? Help! Thanks.
Use configuration files for semi-static values, like connection strings, tcp/ip ports. For other settings that should be changed on the fly, use, for example, a ConfigurationTable.
The process actually has to stop and restart to read in new config values. The config values are read in the first time they're accessed and cached in a dictionary for the lifetime of the application.
According to Microsoft though, changing the config file and saving should trigger restarting the application.
If that doesn't work you should be able to just stop and start the app pool hosting your site or issue an IIS reset.

Configure log4net logging in dll

I'm setting up a dll to be used as a third party dll for a different application. I want this dll to have it's own logging so the external application doesn't have to deal with setting up anything (I don't believe they use the same logging as we do). I've read that may not be the best solution but it's the task I've been given. We want to use log4net with this. I've looked at a few of the other questions on here and they mention that it is configurable via code, however, the main issue I'm having is that there is no clear cut entry point into our code to configure log4net. I'm curious if I should just abandon having the dll configure itself and have a method that is called by the secondary application that configures the dll's logging or if there is a better way to go about this. Any input would be much appreciated
You can configure log4net programmatically. Perhaps add this code to the constructor of your DLL.
if (!log4net.LogManager.GetRepository().Configured)
{
// my DLL is referenced by web service applications to log SOAP requests before
// execution is passed to the web method itself, so I load the log4net.config
// file that resides in the web application root folder
var configFileDirectory = (new DirectoryInfo(TraceExtension.AssemblyDirectory)).Parent; // not the bin folder but up one level
var configFile = new FileInfo(configFileDirectory.FullName + "\\log4net.config");
if (!configFile.Exists)
{
throw new FileLoadException(String.Format("The configuration file {0} does not exist", configFile));
}
log4net.Config.XmlConfigurator.Configure(configFile);
}

How can I debug this web service?

I have a simple web service that looks something like this:
[WebMethod]
public OrderForecastItem GetOrderForecast(int shipTo, string catalogName, bool showPricing)
{
return OrderForecastManager.GetOrderForecast(shipTo, catalogName, showPricing);
}
I'm calling it from another place in a fairly simple way:
using (OrderForecastWS.OrderForecastWS service = new OurSite.Web.Reporting.OrderForecastWS.OrderForecastWS())
{
OrderForecastItem orderForecastItems = service.GetOrderForecast(2585432, "DENTAL", false);
}
After some gymnastics to get the systems to understand that I'm talking about the same type of objects on the client and server sides (I had to open the Reference.cs file inside my Web References, delete the generated OrderForecastItem and add a link to our real OrderForecastItem), the system runs and attempts to get the item.
Unfortunately, it now bombs during the service call, claiming:
Exception There is an error in XML document (1, 1113).(InvalidOperationException)
I can go to the web service in a browser, put in the same values, and I get a seemingly valid XML response. (It looks okay to me, and I ran it through the XML parser at W3Schools, and it cleared it.)
I think the numbers in the error are supposed to be the line and character number...but the browser seems to reformat the xml document, so I can't easily see what the original (1, 1113) location is.
I don't see an easy way to intercept the response and examine it, since it seems to be blowing up as soon as it gets it.
How can I debug this?
If you control the service, you can step into it. If it is a part of your solution and hosted in VS WebDev on your local box, just F11 from Visual Studio, if service is hosted remotely (eg by IIS on other computer) run remote debugging tool on service host msdn, and then you will be able to step in to the service remotely.
By the way, you can tell Visual Studio to re-use objects from referenced libraries for types from the service: just pick configure Service Reference from service context menu and tell which libraries to re-use.
On second thought this error may happen if returned XML could not be deserialized into your business object. May happen when class is changed on either side, or you are trying to use different version of business library than service is using.
If you use Firefox, I'd recommend Firebug. You'll be able to easily view the response from the website in its original format, which might get you closer to the line and position you're looking for.

Categories

Resources