PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, September 12, 2022

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

 September 12, 2022     .net-5, c#, cross-platform, event-log, preprocessor-directive     No comments   

Issue

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



Answered By - Eriawan Kusumawardhono
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing