0

New project with .net 5.

I tried solutions from : Preprocessor directive in C# for importing based on platform

All attempts where I modify the project file results in an unloadable project. I have to revert back to have a valid project.

This is the code I want to compile/run. Please note that Windows Event is only accessible on Windows plateform and I want to use it mainly for initialization where the file system could be innaccessible directly from the app, for any reason.

#if WINANY
        /// <summary>
        /// Will log a Windows Event. Event are visible in the Application section with EventViewer.
        /// </summary>
        public static void LogWindowsEvent(LogType logType, string message, Exception ex = null)
        {
            string appName = AppInfo.AppName;
            if (!EventLog.SourceExists(appName))
            {
                EventLog.CreateEventSource(appName, "Application");

                if (logType == LogType.LogException)
                {
                    EventLog.WriteEntry(appName, $"Exception happen: {ex} at StackTrace: {ex.StackTrace}.", EventLogEntryType.Error);
                }
                else
                {
                    EventLogEntryType eventLogEntryType = EventLogEntryType.Error;
                    switch (logType)
                    {
                        case LogType.LogWarning:
                            eventLogEntryType = EventLogEntryType.Warning;
                            break;
                        case LogType.LogMessage:
                            eventLogEntryType = EventLogEntryType.Information;
                            break;
                    }

                    EventLog.WriteEntry(appName, message, eventLogEntryType);
                }
            }
        }

#else
        /// <summary>
        /// 2021-08-30, EO: Empty for the moment (Until I find a solution).
        /// </summary>
        public static void LogWindowsEvent(LogType logType, string message, Exception ex = null)
        {
        }
#endif

I tried to include these 2 solutions without success. As soon as I change the project file, the project won't load.

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
      <DefineConstants>WIN64;WINDOWS;$(DefineConstants)</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
      <DefineConstants>WIN64;WINDOWS;$(DefineConstants)</DefineConstants>
  </PropertyGroup>
      <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
          <DefineConstants>WIN32;WINDOWS;$(DefineConstants)</DefineConstants>
      </PropertyGroup>
      <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
          <DefineConstants>WIN32;WINDOWS;$(DefineConstants)</DefineConstants>
      </PropertyGroup>

Second attempt:

  <PropertyGroup Condition="$(Platform) == 'x64'">
      <DefineConstants>WIN64;WINANY;$(DefineConstants)</DefineConstants>
  </PropertyGroup>
  <PropertyGroup Condition="$(Platform) == 'x86'">
      <DefineConstants>WIN32;WINANY;$(DefineConstants)</DefineConstants>
  </PropertyGroup>
  

Edit 2021-08-31, 16h49 EST

Eriawan answer seems to be promissing but it didn't work for me, all the code stay gray either when I'm setup for "x64". I don't know why. Perhaps because I use .net 5? Also, when I type "#if", intellisense show me some variables but "WINDOWS" is not there. I don't know if all possibilities should be there, perhaps some could be missing. But "WINDOWS" definitively does not works.

Eric Ouellet
  • 10,996
  • 11
  • 84
  • 119
  • https://blog.magnusmontin.net/2018/11/05/platform-conditional-compilation-in-net-core/ – TheGeneral Aug 30 '21 at 22:00
  • ok, what OS do you want to _actually_ support? if you want to support not just Windows, then you should update your project to have multiple TFM defined (including Windows). Example: `net5.0;net5.0-windows;net5.0-linux` will give you intelisense symbol of `WINDOWS`. – Eriawan Kusumawardhono Aug 31 '21 at 21:09
  • @EriawanKusumawardhono, Thanks a lot. You are surely right. The problem is that I'm designing a library which could be use on any platform but I do not want to specify specific target like "net5.0-windows". Doing so force any app that want to use my library to include the same dependency on net5.0-windows which I exactly does not want to do. – Eric Ouellet Sep 01 '21 at 18:06

1 Answers1

1

There is no WINANY constant to determine OS platform your code runs on. Since .NET 3.1, the available common constants are WINDOWS, ANDROID, LINUX, IOS.

Your code above can be simplified to this:

        /// <summary>
        /// Will log a Windows Event. Event are visible in the Application section with EventViewer.
        /// </summary>
        public static void LogWindowsEvent(LogType logType, string message, Exception ex = null)
        {
#if WINDOWS
            string appName = AppInfo.AppName;
            if (!EventLog.SourceExists(appName))
            {
                EventLog.CreateEventSource(appName, "Application");

                if (logType == LogType.LogException)
                {
                    EventLog.WriteEntry(appName, $"Exception happen: {ex} at StackTrace: {ex.StackTrace}.", EventLogEntryType.Error);
                }
                else
                {
                    EventLogEntryType eventLogEntryType = EventLogEntryType.Error;
                    switch (logType)
                    {
                        case LogType.LogWarning:
                            eventLogEntryType = EventLogEntryType.Warning;
                            break;
                        case LogType.LogMessage:
                            eventLogEntryType = EventLogEntryType.Information;
                            break;
                    }

                    EventLog.WriteEntry(appName, message, eventLogEntryType);
                }
            }
#else
          // Empty code for non Windows
#endif
        }

For more information, see also this official document on .NET Core design document:

https://github.com/dotnet/designs/blob/main/accepted/2020/net5/net5.md#scenarios-and-user-experience

Eriawan Kusumawardhono
  • 4,796
  • 4
  • 46
  • 49
  • Thank you very much. The code seems perfect and to place it inside the backet is also very nice. But it does not work for me. The code stay grey either if I'm setup for x64. Do you know if it could be because it's .net 5 code? – Eric Ouellet Aug 31 '21 at 20:33
  • 1
    Why it does not work? If you want to set up for 64bit or not, this is different problem. Your initial question was to ensure that your code should only runs on Windows, not on other OS. My answer also applies on .NET Core 3.0, 3.1, not just 5.0. – Eriawan Kusumawardhono Aug 31 '21 at 20:59