I'm currently working with two projects: a .NET Standard 2.0 class library and a .NET Framework 4.6.2 console application. In my .NET Standard library, I'm using the System.Diagnostics.EventLog library.
I wrote a method in .NET Standard project:
public static void LogTest()
{
if (!EventLog.SourceExists(System.AppDomain.CurrentDomain.FriendlyName))
EventLog.CreateEventSource(System.AppDomain.CurrentDomain.FriendlyName, "Application");
}
However, when I attempt to call the LogTest method from the .NET Framework console application, I encounter an error stating System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Diagnostics.EventLog, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.'
I was under the impression that the .NET Framework project should automatically copy these dependencies from the .NET Standard library, but it doesn't. So I added
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
line to NET Standard 2.0 class library csproj, it copies the file to build directory of Net Standard project, but still .NET Framework does not have System.Diagnostics.EventLog dll in build directory, so I made their build directory the same, but now I am getting:
System.PlatformNotSupportedException: 'EventLog access is not supported on this platform.'
exception. When I investigated, I saw that the System.Diagnostics.EventLog.dll is for .Net Standard 2.0 in build directory. I copied System.Diagnostics.EventLog.dll for .NET Framework 4.6.2 from Nuget directory (C:\Users\XXX.nuget\packages\system.diagnostics.eventlog\8.0.0).
I could not find a document for this problem, and asked Github Copilot, and it said add the nuget package also to the NET Framework project, and it seems it solved the problem, but this is not a good solution, because I have to know every nuget package references in the other projects.
I uploaded an example project here: https://github.com/sahin52/NetStandardInNetFrameworkProjExcample you can build and see the same problems, and ClassLibrary1\ClassLibrary1.csproj has a line with CopyLocalLockFileAssemblies, which is commented out currently, you can test it by removing comment tags also.
How can I solve this problem in a nice way? Thanks in advance.
Edit: This is not only related to this package, I also got the same error for ModifyRegistry: System.PlatformNotSupportedException: Registry is not supported on this platform..
Try this instead you can use the .NET Platform Extensions. This allows you to use Windows-specific libraries in a .NET Core project without having to go through .NET Standard.
First you need the Windows Compatibility Pack.
Then you can install the EventLog library.