Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PublishSingleFile causes Target symbol-name is unknown: 'RichTextBox' #99

Closed
lionengine123 opened this issue Mar 24, 2022 · 16 comments
Closed
Labels

Comments

@lionengine123
Copy link

lionengine123 commented Mar 24, 2022

Hello,

I use NLog.Windows.Forms (version 4.5.0).
Whenever a Project is published with this property:

<PublishSingleFile>true</PublishSingleFile>

I get such an error: (internalLogfile.log)

Info Message Template Auto Format enabled
Info Adding target FileTarget(Name=logfile)
Warn Error has been raised. Exception: NLog.NLogConfigurationException: Failed to create Target of type: RichTextBox
 ---> System.ArgumentException: Target symbol-name is unknown: 'RichTextBox'
   at NLog.Config.Factory`2.CreateInstance(String itemName)
   at NLog.Config.LoggingConfigurationParser.FactoryCreateInstance[T](String classType, INamedItemFactory`2 factory)
   --- End of inner exception stack trace ---
Warn Error has been raised. Exception: NLog.NLogConfigurationException: Failed to create Target of type: RichTextBox
 ---> System.ArgumentException: Target symbol-name is unknown: 'RichTextBox'
   at NLog.Config.Factory`2.CreateInstance(String itemName)
   at NLog.Config.LoggingConfigurationParser.FactoryCreateInstance[T](String classType, INamedItemFactory`2 factory)
   --- End of inner exception stack trace ---
Warn Error has been raised. Exception: NLog.NLogConfigurationException: Target 'warningControl' not found for logging rule: *.
Warn Error has been raised. Exception: NLog.NLogConfigurationException: Target 'logControl' not found for logging rule: *.
Info Validating config: TargetNames=logfile, ConfigItems=23, FilePath=D:\development\branches\EFA_10_5\realtime\Solutions\DDIP\Applications\Mdv.DDIP.BatchWork.GUI\bin\release\win-x64\publish\NLog.config
Info Configuration initialized.
Info NLog, Version=5.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c. File version: . Product version: . GlobalAssemblyCache: False
Info Executing ReInitializeAllTextboxes for Form Mdv.Realtime.BatchWork.GUI.MainWindow, Text: DDIP v.10.5.8.1 Batch work GUI [new]
Info Configuring from an XML element in D:\development\branches\EFA_10_5\realtime\Solutions\DDIP\Applications\Mdv.DDIP.BatchWork.GUI\bin\release\win-x64\publish\NLog.config...
Info Message Template Auto Format enabled
Info Adding target FileTarget(Name=logfile)
Warn Error has been raised. Exception: NLog.NLogConfigurationException: Failed to create Target of type: RichTextBox
 ---> System.ArgumentException: Target symbol-name is unknown: 'RichTextBox'
   at NLog.Config.Factory`2.CreateInstance(String itemName)
   at NLog.Config.LoggingConfigurationParser.FactoryCreateInstance[T](String classType, INamedItemFactory`2 factory)
   --- End of inner exception stack trace ---
Warn Error has been raised. Exception: NLog.NLogConfigurationException: Failed to create Target of type: RichTextBox
 ---> System.ArgumentException: Target symbol-name is unknown: 'RichTextBox'
   at NLog.Config.Factory`2.CreateInstance(String itemName)
   at NLog.Config.LoggingConfigurationParser.FactoryCreateInstance[T](String classType, INamedItemFactory`2 factory)
   --- End of inner exception stack trace ---
Warn Error has been raised. Exception: NLog.NLogConfigurationException: Target 'warningControl' not found for logging rule: *.
Warn Error has been raised. Exception: NLog.NLogConfigurationException: Target 'logControl' not found for logging rule: *.
Info Validating config: TargetNames=logfile, ConfigItems=23, FilePath=D:\development\branches\EFA_10_5\realtime\Solutions\DDIP\Applications\Mdv.DDIP.BatchWork.GUI\bin\release\win-x64\publish\NLog.config
Info AppDomain Shutting down. LogFactory closing...
Info LogFactory has been closed.

I tried the following package constellations:

Nlog 5.0.0-rc2
Nlog.Windows.Forms 4.5.0

and

NLog 4.7.0
Nlog.Windows.Forms 4.5.0-rc1

In case I build the project without this PublishSingleFile property, the logging into RichTextBox works fine.

@304NotModified
Copy link
Member

@lionengine123
Copy link
Author

lionengine123 commented Mar 25, 2022

I added this one code line

NLog.LogManager.Setup().SetupExtensions(s =>s.RegisterAssembly("MyAssembly"));

at the very beginning of the program.cs main method and exchanged 'MyAssembly' with my Assembly name.
However this did not work. It still does not log into the corresponding richtext box when the program is published as single file.

@snakefoot
Copy link
Contributor

Maybe try this:

NLog.LogManager.Setup().SetupExtensions(s => s.RegisterAssembly(typeof(NLog.Windows.Forms.RichTextBoxTarget).Assembly));

@lionengine123
Copy link
Author

lionengine123 commented Mar 26, 2022

yes, this works.

However we have 20 Projects and try to keep it as 'tidy' as possible. In case it would be possible to fix the issue within the future version of nlog windows forms library, it would be great.

System.Configuration.ConfigurationManager had a similiar problem and fixed it with version 6.0.0 btw.

@snakefoot
Copy link
Contributor

snakefoot commented Mar 26, 2022

Have you tried to explicit include the Assembly in your NLog.config:

<nlog> 
  <extensions> 
    <add assembly="NLog.Windows.Forms"/> 
  </extensions> 

@lionengine123
Copy link
Author

lionengine123 commented Mar 27, 2022 via email

@snakefoot
Copy link
Contributor

snakefoot commented Mar 27, 2022

No, my nlog.config does not include any extension entries.

You can replace the explict RegisterAssembly in source-code with <extensions>-section in NLog.config.

Notice NLog 5.0 will no longer scan and load NLog-extension-assemblies automatically. Instead NLog-extension-assemblies must always be explicitly specified. But NLog 5.0 also introduces support for specifying assembly-name in target-type:

<target xsi:type="RichTextBox, NLog.Windows.Forms" name="myRichTextBox" ... />

@lionengine123
Copy link
Author

lionengine123 commented Mar 28, 2022

Have you tried to explicit include the Assembly in your NLog.config:

<nlog> 
  <extensions> 
    <add assembly="NLog.Windows.Forms"/> 
  </extensions> 

Thank you this also works. (So no code changes aka 'RegisterAssembly' necessary.)

@lionengine123
Copy link
Author

You can replace the explict RegisterAssembly in source-code with <extensions>-section in NLog.config.

Notice NLog 5.0 will no longer scan and load NLog-extension-assemblies automatically. Instead NLog-extension-assemblies must always be explicitly specified. But NLog 5.0 also introduces support for specifying assembly-name in target-type:

<target xsi:type="RichTextBox, NLog.Windows.Forms" name="myRichTextBox" ... />

Adding
<target xsi:type="RichTextBox, NLog.Windows.Forms" name="myRichTextBox" ... />

was not sufficient (/ is not needed) when publishing as single file.
There also needs to be this declaration:

<extensions> <add assembly="NLog.Windows.Forms"/> </extensions>

(Tested with 5.0.0-rc2)

@snakefoot
Copy link
Contributor

snakefoot commented Mar 28, 2022

NLog InternalLogger looks like this on my machine, and it just works with NLog 5.0-RC2:

Info Message Template Auto Format enabled
Debug ScanAssembly('NLog, Version=5.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c')
Info Loading assembly: NLog.Windows.Forms
Debug ScanAssembly('NLog.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c')

If I just use xsi:type="RichTextBox" then it fails like you experience.

@lionengine123
Copy link
Author

This thread is all about publishing as single file.
Did you publish your application as Single file?

@snakefoot
Copy link
Contributor

snakefoot commented Mar 28, 2022

This is my csproj-file:

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <UseWindowsForms>true</UseWindowsForms>
    <PublishSingleFile>true</PublishSingleFile>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="NLog" Version="5.0.0-rc2" />
  </ItemGroup>

And this is the command that I was running to perform the publish:

dotnet publish -c Release

This is the publish-folder:

image

This is the NLog.config:

<?xml version="1.0" encoding="utf-8" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.nlog-project.org/schemas/NLog.xsd NLog.xsd">

  <targets>
    <target xsi:type="RichTextBox, NLog.Windows.Forms"
            name="target1"
            layout="${message}"

            autoScroll="true"
            maxLines="30"
     
            useDefaultRowColoringRules="true" />

@lionengine123
Copy link
Author

lionengine123 commented Mar 28, 2022

Thanks,
I was just asking because the program behaviour differs in comparison to our application.
we also build with
dotnet publish -c:Release

However we have: (Net5.0)

<TargetFramework>net5.0-windows</TargetFramework>
and also usually
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>

Perhaps that's where the different behaviour comes from.
As
<target xsi:type="RichTextBox, NLog.Windows.Forms" ... />
doesn't work for us.

Your answer:

Have you tried to explicit include the Assembly in your NLog.config:

<nlog> 
  <extensions> 
    <add assembly="NLog.Windows.Forms"/> 
  </extensions> 

is already our solution and solves the problem on our side.

@lionengine123
Copy link
Author

lionengine123 commented Mar 28, 2022

In your Testsetting I realized you are not using a controlName. Do you log into a separate Logging Window?
We are logging into an existing richtextbox, which is a big difference ...

`

<target name="locatortextbox" xsi:type="RichTextBox" controlName="locatorTextBox" formName="LogForm" maxLines="100" autoScroll="true"
    useDefaultRowColoringRules="true" layout="${longdate} ${uppercase:${level}} ${message}" allowAccessoryFormCreation="False"/>

...
`

(Net5 <-> Net6 didnt make any difference)
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract> also didnt make any difference.

@snakefoot
Copy link
Contributor

snakefoot commented Mar 28, 2022

Think your questions are beyond my skill-set when it comes to the NLog.Windows.Forms-package.

I was mostly interested in whether xsi:type="RichTextBox, NLog.Windows.Forms" worked with NLog 5.0 and PublishSingleFile=true (And it did for me).

And yes when using NLog 4.7 with PublishSingleFile=true, then it will only work with:

<nlog> 
  <extensions> 
    <add assembly="NLog.Windows.Forms"/> 
  </extensions>

@snakefoot
Copy link
Contributor

Closing issue as answered due to inactivity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants