WCF Message Logging without configuration file?

I have a C# service without a configuration file. This is a personal choice, it just feels cleaner to me. So, the VS2008 solution is just one single .cs file. I want to configure message logging, but the MSDN website only says how by configuring the app.config file. Surely there must be a way to add message logging directly within the .cs file?

I don't know if you need it, but I've included the entire .cs file just in case. Let me know if I can add any more information. Thanks!
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;
using System.Xml;
using System.IO;
using System.Web;
using System.Text;
using System.Runtime.Serialization;
using System.ServiceModel.Web;
using System.Net;
using System.Collections.Specialized;
using System.Diagnostics;
 
public class getString
{
    [ServiceContract]  
        public interface ITest  
        {  
            [OperationContract]  
            [WebInvoke]  
            void SaveString(Stream stream);  
        }  
        public class Service : ITest  
        {  
            public void SaveString(Stream text)
            {
                string stringText = new StreamReader(text).ReadToEnd();
                string now = DateTime.Now.ToString();
                Console.WriteLine("\n{0}: contact made...", now);
 
                string nowForFile = DateTime.Now.ToString().Replace('/', '_').Replace(':', '_');
                string fileName = "Result_" + nowForFile + ".xml";
                StreamWriter sw = new StreamWriter(fileName);
                sw.WriteLine(stringText);
                sw.Close();
 
                Console.WriteLine("xml payload saved as file...\n\n");
            }  
        }  
        static Binding GetBinding()  
        {  
            BasicHttpBinding result = new BasicHttpBinding();
            return result;  
        }  
 
    public static void Main()
    {
        string baseAddress = "http://10.2.2.61:8004/Service";
        WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
        host.Open();
        Console.WriteLine("Serivce running at {0}", baseAddress);
        WebClient client = new WebClient();
        client.Headers[HttpRequestHeader.ContentType] = "text/plain";
        Console.WriteLine("Press <ENTER> To Exit...\n\n");
        Console.ReadLine();
        host.Close();
    }
}

Open in new window

ehensensAsked:
Who is Participating?
 
MogalManicConnect With a Mentor Commented:
Yes it is possible to initialize logging W/O using a config file.  The attached code snippit is an example using the System.Diagnostics.Trace.

I use log4net for most of my logging and any logging framework more complex than System.Diagnostics.Trace has lots more settings available and they occasionally change.  The config file allows you to make changes without having to create custom application functionality like you did with the baseAddress.

For example, a simple  Log4Net config file might be something like this:

 
   
   
 


 
   
   
 



What this is saying is Log DEBUG level messages to ErrorLog.log file in the current directory.  This might be fine, but if my requirements change and I want to be the log messages to be sent to the EVENTVIEWER application, then all I have to do is change a file.  

Many of the configuration changes are application changes and they change the behavior of the application.  You don't need to provide a user interface to change the settings because if you aren't skilled enough to edit an XML file, then you probably shouldn't be making the changes.  Other examples in your application that could be pushed into a configuration file are the settings around the WebServiceHost class.  There is the DefaultOpenTimeout and DefaultCloseTimeout for example.  If the users are getting timeout errors, it would be nice to be able to change the values and restart the service to see if that helps.
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
 
namespace SampleLogging
{
    class Program
    {
        static void Main(string[] args)
        {
            using (TraceListener listener = new TextWriterTraceListener("LogMessages.log"))
            {
                Trace.Listeners.Add(listener);
 
                Trace.WriteLine("TestMessage");
            }
        }
    }
}

Open in new window

0
 
MogalManicCommented:
You NEED a config file.   There are parts of your application that would benefit from the ability to change at run time.  I would change the main to:


 public static void Main()
    {
        string baseAddress = ConfigurationManager.AppSettings["BASE_ADDRESS"];
        WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(baseAddress));
        host.Open();
        Console.WriteLine("Serivce running at {0}", baseAddress);
        WebClient client = new WebClient();
        client.Headers[HttpRequestHeader.ContentType] = "text/plain";
        Console.WriteLine("Press <ENTER> To Exit...\n\n");
        Console.ReadLine();
        host.Close();
    }

Open in new window

0
 
ehensensAuthor Commented:
Hi MogalManiac,

Thank you for your input. Can you say how exactly I would benefit from having a config file? Since this post I have added the option to let the user change any aspects of the baseAddress he/she wishes directly within the console.

But back to the question, can I implement message logging without a config file?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.