Search code examples
c#cross-platform.net-5event-logpreprocessor-directive

How to compile code (choose part of the code) according to the target platform?


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.


Solution

  • 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