Take an existing MvvmCross project with a UWP project that was working in MvvmCross 5.3.2 (or create a new one using the documentation here https://www.mvvmcross.com/documentation/platform/universal-windows-platform-uwp/universal-windows-platform-uwp [with the only changes needed to the project that is created is to update the namespaces to reflect the current ones being used as the documentation is a little out of date]) and then upgrade it to MvvmCross 5.4
Run the project
The app should load up as expected
The following exception is thrown: System.TypeInitializationException: 'The type initializer for 'MvvmCross.Core.Platform.LogProviders.ConsoleLogProvider' threw an exception.'
Inner Exception
InvalidOperationException: System.Console or System.ConsoleColor type not found
Version: 5.4
Platform:
Note: This happens regardless of whether I include the DebugTrace class or not. This seems to be in the MvvmCross framework itself and I think it might have to with the fact that System.Console isn't available to UWP projects (I don't think?).
This will be fixed in 5.4.1
A workaround can be found here
Awesome, thanks! :)
FWIW, if you wanna use the Console provider with IMvxLog, simply include this in your LinkerPleaseInclude.cs:
using System;
//[...]
public void Include(ConsoleColor color)
{
Console.Write("");
Console.WriteLine("");
color = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.ForegroundColor = ConsoleColor.Magenta;
Console.ForegroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Gray;
Console.ForegroundColor = ConsoleColor.DarkGray;
}
It looks like this is now fixed for iOS and Android but still fails for me in UWP.
UWP seems not to know about System.Console.ForegroundColor.
Any workaround?
Yea, MVVMCross 5.4.2 still has the same issue as 5.4.0 did. The workaround of disabling the linker by override the return of the GetDefaultLogProviderType() to "MvxLogProviderType.None" still works, but the actual issue for UWP projects isn't fixed as far as I can tell.
I also tried to add the suggested LinkerPleaseInclude class to my UWP project, but the Console class doesn't exist in UWP, which I think is the problem.
So I think this issue might need to be reopened.
@willsb as System.Console is not available on UWP I think we need a different fix for this issue, am I right?
If you update to Microsoft.NETCore.UniversalWindowsPlatform 6.0.1 it is available.
There might be a conflict if you also use splat. This can be solved by adding:
<Target Name="ChangeAliasesOfStrongNameAssemblies" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
<ItemGroup>
<ReferencePath Condition="'%(FileName)' == 'Splat'">
<Aliases>SplatAlias</Aliases>
</ReferencePath>
</ItemGroup>
</Target>
to your .csproj file
This will change Splat's namespace to:
```c#
using SplatAlias::Splat;
After that:
```c#
using System;
//[...]
public void Include(ConsoleColor color)
{
Console.Write("");
Console.WriteLine("");
color = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.ForegroundColor = ConsoleColor.Magenta;
Console.ForegroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Gray;
Console.ForegroundColor = ConsoleColor.DarkGray;
}
will do the job together with
c#
protected override MvxLogProviderType GetDefaultLogProviderType()
{
return MvxLogProviderType.None;
}
in Setup.cs
I just figured it out half an hour ago. :-)
MvxLogProviderType.None alone should suffice, since the console logger won't even be instantiated then.
So I've done a bit more reading on this and have the following additional information/thoughts:
So, based on this information, I'm predicting that this means that once MVVMCross is built as a .NET Standard library (provided 1.3 or higher is chosen as what it is built against) then this problem will fix itself.
I did test creating a .NET Standard 1.4 library as a shared project and adding MVVMCross via the fallback trick, but still got the same issue. I haven't tested rebuilding MVVMCross as a .NET Standard library myself (as I don't really have the time or skills to try to do that just to test this theory), but it makes sense in my head that this would fix this issue (happy to be told if I've made a mistake in my assumptions somewhere though)
So until this happens, I'll just continue to add the workaround code to turn off the logging (not an issue for me at all, the logging isn't important to me). The only problem I see is that unfortunately the default experience for consumers of MVVMCross on UWP projects is they're going to get this runtime error straight out of the box, unless they know to disable the logging component for now.
You are correct, when we migrate to netstandard this will be fixed (since we won't have to rely on reflection anymore).
It would also be possible to install System.Console from nuget. This is solved in 6.0
Most helpful comment
FWIW, if you wanna use the Console provider with
IMvxLog, simply include this in yourLinkerPleaseInclude.cs: