0

It seems that Debug.WriteLine does not work in Visual Studio 2017 Community Edition, as discussed in the following link: Console.WriteLine does not output to Output Window in VS 2017.

Can anyone suggest an alternative for a Xamarin Forms PCL? When I look at the Debug object from the PCL I only see methods for WriteLine, so I am struggling to see how to attach to a different Listener.

When I look at the the Xamarin documentation for the System.Diagnostics namespace it lists lots of methods and classes that I don't seem to have access to from within my PCL. For example, the 'TraceListener' class is missing. Is that correct behaviour?

hvaughan3
  • 10,955
  • 5
  • 56
  • 76
Ant Waters
  • 510
  • 1
  • 5
  • 22

1 Answers1

1

System.Diagnostic.Debug.WriteLine() works fine for me in a PCL project, a .NET Standard project, and Xamarin.Android/Xamarin.iOS projects using Visual Studio 2017.

Have you tried it yourself?

System.Console.WriteLine() however does not work in a PCL project, though that has nothing to do with the IDE you are using and has more to do with how PCL projects work, but does work in a Xamarin.Android/Xamarin.iOS project. If you are wanting use System.Console.WriteLine() in a PCL project you can tap into the native code by various methods. One of those would be a DependencyService.Get<>().

Create the following classes:

PCL:

public interface IPlatformHelpers {
    void WriteLine(string text);
}

iOS & Android:

[assembly: Dependency(typeof(PlatformHelpers))]

namespace App.<iOS | Android> {

    public class PlatformHelpers : IPlatformHelpers {

        public void WriteLine(string text) {
            System.Console.WriteLine(text);
        }
    }
}

Now in your PCL you can do this:

IPlatformHelpers helper = DependencyService.Get<IPlatformHelpers>();

helper.WriteLine("Angry BLAH!");

Interestingly, it appears that System.Console.WriteLine() might be available in a .NET Standard project (at least for .NET Standard 1.3+) though I have not actually tried it out myself yet.

hvaughan3
  • 10,955
  • 5
  • 56
  • 76
  • Yes I have surely tested it. What version of Visual Studio are you using? I think the problem might be peculiar to Visual Studio 2017 Community Edition, as discussed in the link I mentioned above. – Ant Waters May 28 '17 at 11:41
  • Your dependency service approach might be the workaround I need, and seems to work for an Android project. Indeed, in the Android implementation I can use either 'System.Console.WriteLine' or 'Android.Util.Log.Debug'. They both produce output in the VS Output window. However, I can't find anything to work in a UWP project. There doesn't seem to be an equivalent of 'System.Console.WriteLine' that I can find in UWP. If use 'System.Diagnostics.Debug.WriteLine' in the UWP implementation I don't get any output. Any ideas? – Ant Waters May 28 '17 at 12:02
  • @AntWaters I am running VS 2017 Professional and do not have experience with UWP. Have you tried using `Trace.WriteLine()`? Also `Debug.WriteLine()` and `Trace.WriteLine()` are controlled by what is configured in the project properties. Right-click your UWP Project -> Properties -> Build -> then look for **Define DEBUG constant** (which should be checked for DEBUG configurations and unchecked for RELEASE configurations) and **Define TRACE constant** (which should be check for both DEBUG and RELEASE configurations). You say "there doesn't seem to be an equivalent", does that mean it wont build? – hvaughan3 May 28 '17 at 16:55
  • Thanks @hvaughan3 for the suggestions but no progress. The two properties were already checked. yes, I mean that if I try to use `Console.WriteLine` in the UWP project it won't compile, and intellisense can't find any reference to include. The same applies to `Trace`. Conversely, `Debug.WriteLine` is found, and compiles, but doesn't produce any output. – Ant Waters May 28 '17 at 20:26