Click here to Skip to main content
15,887,453 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a library of methods and extension methods that I've been adding to projects for the past 10 months, as I really to learn C# by writing it. But I've added it to a console app project this time.

I'm struggling the past two days with researching why either (1) Debug.WriteLine isn't outputting any of my error reporting and debug code to output screen when everything I'm reading says that it should or (2) My code is not executing in my external class and so there's nothing for the Debug code to write.

Without getting anything back from my watches or from error logging to the output screen, I'm flying blind.

I don't know if console apps are different from, say, windows forms or if it's one of the new constructs I'm trying to use in C#, but I have read and re-read the sections about classes in my C# 7 in a Nutshell book and am overwhelmed, I suppose.

Can someone suggest something for me to read about or investigate?

I don't know what code to post because I don't know what is relevant to this, but here's some code that definitely isn't working as expected:

NOTE: I'm sure my code is atrocious to most of you! Any suggestions are appreciated.

This is part of the class that sets up my OleDb Database Connection:
	public class RBDataOleDb
	{

		private string nl = Environment.NewLine;

		public string DbPassword { get; }
		public string DbUser { get; }
		public string DbType { get; }
		public string DbServer { get; }
		public string DbName { get; }
		public string DbDataSource { get; }
		public string DbPath { get; }


		/// <summary>
		/// Connect to SQLServer or Oracle with windows auth
		/// NOTE: Add User and Password via overloaded constructors below
		/// </summary>
		/// <param name="dbserver">Server EX: @"(localdb)\MSSQLLocalDB"</param>
		/// <param name="dbname">Database name</param>
		public RBDataOleDb ( string server , string database)
		{
			DbType = "MDF";
			DbServer = server;
			DbName = database;
			DbDataSource = database;
		}


		/// <summary>
		/// Connect to SQLServer or Oracle with password
		/// NOTE: Add User and Password seperately
		/// </summary>
		/// <param name="dbserver">Server name in @"(localdb)\MSSQLLocalDB" format or null</param>
		/// <param name="dbname">Database name</param>
		public RBDataOleDb ( string server , string database, string username, string password )
		{
			DbType = "MDF";
			DbServer = server;
			DbName = database;
			DbUser = username;
			DbPassword = password;
			DbDataSource = database;
		}


		/// <summary>
		/// Use this for connections to files, such as MS ACCESS, EXCEL, etc.
		/// </summary>
		/// <param name="file_path">Path to Database</param>
		public RBDataOleDb ( string file_path )
		{
			DbType = System.IO.Path.GetExtension ( file_path ).Replace ( "." , "" ).ToUpper ( );
			DbPath = file_path;
			DbDataSource = file_path;
		}


		/// <summary>
		/// Use this for connections to files, such as MS ACCESS, EXCEL, etc.
		/// </summary>
		/// <param name="file_path">Path to Database</param>
		public RBDataOleDb ( string file_path, string username, string password)
		{
			DbType = System.IO.Path.GetExtension( file_path ).Replace(".","").ToUpper ( );
			DbPath = file_path;
			DbUser = username;
			DbPassword = password;
			DbDataSource = file_path;
		}

		public string GetProvider ( )
		{
			string result = "";
			switch ( DbType )
			{
				case "MDF":
					result = "SQLNCLI11";
					break;
				case "MDB":                  
				case "ACCDB":
				case "XLSX":
				case "XLS":
				case "DBF":
					result = "Microsoft.ACE.OLEDB.12.0";
					break;
				default:
					result = "!!UNABLE TO DETERMINE PROVIDER!!";
					throw new ArgumentException ( $"DbType has a value of {DbType}, which is not valid.  RBDataOleDb clould not determine what database provider to use when connecting to database" );				
			}
			return result;
		}

		/// <summary>
		/// NOTE: Provider: "Microsoft.ACE.OLEDB.12.0" is the 2010 Office System Driver, which has both 32 bit and 64 bit versions that let your app connect 
		/// to Access, Excel and Text file in a 64bit environment.
		/// </summary>
		/// <returns></returns>
		public string GetConnString ( )
		{
			string conn = "";


			if ( DbServer != null )
			{
				conn = $"Provider=SQLNCLI11;Data Source={DbServer};Initial Catalog=[{DbName}];";
				if ( !string.IsNullOrEmpty ( DbPassword ) )
					conn += $"User ID={DbUser};Password={DbPassword};";
				else
					conn += $"Integrated Security=SSPI;";             
			}
			else if ( DbType == "MDB" || DbType == "ACCDB" )
			{
				conn = $"Provider=Microsoft.ACE.OLEDB.12.0;data source=[{DbPath}];";
				if ( !string.IsNullOrEmpty ( DbPassword ) ) conn += $"Jet OLEDB:Database Password={DbPassword};";

			}
			else if ( DbType == "MDB" )
			{
				conn = $@"Provider=Microsoft.Jet.OLEDB.4.0;data source=""{DbPath}"";";
				if ( !string.IsNullOrEmpty ( DbPassword ) ) conn += $"User Id={DbUser};Jet OLEDB:Database Password={DbPassword};";
			}
			else if ( DbType == "XLSX" )
			{
				conn = $@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=[{DbPath}];Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1'";
			}
			else if ( DbType == "XLS" )
			{		conn = $@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=[{DbPath}];Extended Properties='Excel 8.0;HDR=YES'";                    
			}
			else
			{
				conn = "* * * NO DbType WAS SET SO RBDataOleDb.getConnString() UNABLE TO CREATE CONNECTION STRING * * *";
			}
			return conn;
		}
}


This is how I am instantiating and returning values from some of it's properties and methods:

RBDataOleDb ole = new RBDataOleDb ( Globals.dbpath );
string provider = ole.GetProvider();
string ds = ole.DbDataSource;
string constring = ole.GetConnString();

Globals.Log ( ds );
Globals.Log ( provider );
Globals.Log ( constring );


This is my Globals.Log method, which has been edited to write to Console.

public static void Log ( string message )
{
    string output = Environment.NewLine + message + Environment.NewLine;
    Console.WriteLine ( output );
}


What I have tried:

I've rewritten some of my debug logging in my local project so that I can write to Console instead of Debug but I cant' do that from classes in my library or it will effect my other projects that are using it.

As far as getting values back form my class, I've tried using methods, different ways of using getters and setters, and changed access modifiers several times. What you see if the result of those iterative efforts.
Posted
Updated 14-Jan-18 11:52am

1 solution

I'm answering this question under the assumption you want Debug.WriteLine to output to the console. Thats how i understood your question. If you need to output elsewhere other than the console read up on Debug listeners (link below).

Quote:
I'm struggling the past two days with researching why either (1) Debug.WriteLine isn't outputting any of my error reporting and debug code to output screen when everything I'm reading says that it should or (2) My code is not executing in my external class and so there's nothing for the Debug code to write.


These are the likely causes for your debug.writeline code not outputting any data.

1) Your solution config is not set to "Debug" but rather something else like Release.
2) You need to set your debugging to NOT redirect all output. Do this in visual studio by going to Menu -> Tools -> Options -> Window pops up -> Scroll to Debugging -> General -> Look for "Redirect all output window text to the immediate window" and make sure that isn'
t checked. To be clear, is not checked.
3) You don't have a listener setup for Debug.WriteLine. See this link for more info: Debug.Listeners Property (System.Diagnostics)[^]

Essentially you just need to include something like

C#
var dbglistener = new TextWriterTraceListener(Console.Out);
Debug.Listeners.Add(dbglistener);


For your Debug.WriteLine to start outputting to the console window.

If you need to output debug messages to something other than the console you can output those messages to a Stream and then from that Stream write the data to a file.

Also. Console apps are very different from winforms in terms of use/purpose. If you were to keep writing output to a console using the TextWriterTraceListener class it would appear to not work in winforms as winforms does not utilize a console window for output. For winforms you'd either need to do as I mentioned which is write output to a stream or looking into a logging framework like nlog or log4net...something like that.
 
Share this answer
 
v3
Comments
[no name] 14-Jan-18 18:43pm    
I messed around with the Debug vs. Release setting once before, but checked it and it is set to Debug... OR SO I THOUGHT! Typical Microsoft, the solution configuration screen shows Debug everywhere and only one Project Context, which is set to Debug, when I just clicked into Configuration Manager button, I see that it's current, active configuration is RELEASE. Well that's lovely. I ripped half my program apart and everything I was not totally familiar with. Ugh. Thank you for your help. I'm a bit discouraged at the moment but will look at your other suggestions, just so I know how to troubleshoot this type of issue.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900