Obfuscar for asp.net core 3.1 - obfuscar

In the ASP.NET Core 3.1 app, the given path in the obfuscar.xml is correct but it is not able to find the assembly/Failed to resolve assembly:
<?xml version='1.0'?>
<Obfuscator>
<Var name="InPath" value=".\bin\Debug\netcoreapp3.1" />
<Var name="OutPath" value="$(InPath)\publish" />
<Var name="KeepPublicApi" value="false" />
<Var name="HidePrivateApi" value="true" />
<Var name="RenameProperties" value="true" />
<Var name="RenameEvents" value="true" />
<Var name="RenameFields" value="true" />
<Var name="UseUnicodeNames" value="true" />
<Var name="HideStrings" value="true" />
<Var name="OptimizeMethods" value="true" />
<Var name="SuppressIldasm" value="true" />
<Module file="$(InPath)\Sample.dll" />
</Obfuscator>
The build error:
4>Note that Rollbar API is enabled by default to collect crashes. If you want to opt out, please run with -s switch
4>Loading project .\obfuscar.xml...
4>An error occurred during processing:
4>Unable to find assembly: .\bin\Debug\netcoreapp3.1\Sample.dll
4>Failed to resolve assembly: 'Microsoft.AspNetCore.Mvc.Core, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'
4>G:\SomePath\Sample.csproj(110,5): error MSB3073: The command "if Debug == Debug obfuscar.console .\obfuscar.xml" exited with code 1.
4>Done building project "Sample.csproj" -- FAILED.

This combination worked for me on netcoreapp3.1
Build Command
Make sure you use publish as well as --self-contained and -r to ensure all necessary dependencies are copied to the output directory.
dotnet publish -c Release --self-contained=true -r win-x64 <project.csproj>
project.csproj
<ItemGroup>
<Content Include="obfuscar.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Obfuscar">
<Version>2.2.14</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<PropertyGroup>
<PostBuildEvent>$(Obfuscar) obfuscar.xml</PostBuildEvent>
</PropertyGroup>
Obfuscar.xml
<?xml version='1.0'?>
<Obfuscator>
<Var name="InPath" value="." />
<Var name="OutPath" value=".\Obfuscator_Output" />
<Var name="KeepPublicApi" value="true" />
<Var name="HidePrivateApi" value="true" />
<Var name="RenameProperties" value="true" />
<Var name="RenameEvents" value="true" />
<Var name="RenameFields" value="true" />
<Var name="UseUnicodeNames" value="true" />
<Var name="HideStrings" value="true" />
<Var name="OptimizeMethods" value="true" />
<Var name="SuppressIldasm" value="true" />
<Module file="$(InPath)\Project.dll" />
</Obfuscator>

Use
<AssemblySearchPath path="C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\5.0.5" />

Related

Install4j: Executable file deleted when reboot was performed after the installation in Windows Server 2012 64bit

I generated the installer executable file in Windows 7 64-bit using install4j 6.1.1. Then the executable file was copied to a Windows server 2012 machine and the software was installed. After the installation, the windows was rebooted (no requirement of my software installation process) at any random time, but after the reboot, the installer file vanished! I have retested it keeping the installer in different locations but every time I install the software and then perform a reboot, weirdly the installer is deleted.
Following is the media-file setting inside the install4j project:
<windows name="Windows-64" id="24" customizedId="win64" mediaFileName="${compiler:sys.shortName}_${compiler:sys.version}_Windows_x64" installDir="/backend" overridePrincipalLanguage="false" jreBitType="64" runPostProcessor="false" postProcessor="" failOnPostProcessorError="false" useLegacyMediaFileIds="false" legacyMediaFileIds="" downloadURL="" includeAllDownloadableComponents="false" includedJRE="resources/windows-amd64-1.7.0_79" manualJREEntry="false" bundleType="1" jreURL="" jreShared="false" directDownload="false" installOnlyIfNecessary="false" customInstallBaseDir="C:/" contentFilesType="1" verifyIntegrity="true">
<excludedComponents />
<includedDownloadableComponents />
<excludedLaunchers />
<excludedBeans>
<bean refId="3770" />
<bean refId="3771" />
<bean refId="1594" />
<bean refId="3772" />
<bean refId="1737" />
<bean refId="3773" />
</excludedBeans>
<overriddenPrincipalLanguage id="en" customLocalizationFile="" />
<exclude>
<entry location="admin/bin/teamviewer_11.0.57095.i686.rpm" fileType="regular" />
<entry location="bin/fpcsctl" fileType="regular" />
<entry location=".i4j_external_105/sql/initDB.sh" fileType="regular" />
<entry location=".i4j_external_105/temp/jdk1.7.0_79_linux-32.zip" fileType="regular" />
<entry location=".i4j_external_105/temp/jdk-7u79-windows-x32.zip" fileType="regular" />
<entry location=".i4j_external_105/temp/postgresql-9.4.6-linux-32.zip" fileType="regular" />
<entry location=".i4j_external_105/temp/postgresql-9.4.6-linux-64.zip" fileType="regular" />
<entry location=".i4j_external_105/temp/postgresql-9.4.6-windows-x86.zip" fileType="regular" />
<entry location=".i4j_external_105/temp/vcredist_x86.exe" fileType="regular" />
<entry location=".i4j_external_105/temp/jdk1.7.0_79_linux-64.zip" fileType="regular" />
<entry location=".i4j_external_105/temp/librxtxSerial.so" fileType="regular" />
<entry location=".i4j_external_105/temp/librxtxParallel.so" fileType="regular" />
</exclude>
<variables />
<autoUpdate useMinUpdatableVersion="false" minUpdatableVersion="" useMaxUpdatableVersion="false" maxUpdatableVersion="">
<commentFiles />
<customAttributes />
</autoUpdate>
</windows>
This is bug that was fixed in install4j 6.1.2.

How to stop Log4Net from creating an empty log file

I have stopped all appenders from writing logs to a file by adding <log4net threshold="OFF" />. Now I want to stop Log4net from creating the empty log file. Any idea how? Thanks in advance!
RootAppender:
<appender name="RootRollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="D:\Documents\DEBUG.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="2" />
<maximumFileSize value="10000KB" /> ...
</appender>
I managed to stop log4net from creating the empty log file by removing the <file > child element of the appender, along with the related properties, and setting <appendToFile value="false" />. The modified configuration is as follows:
<log4net threshold="OFF">
...
<appendToFile value="false" />
<rollingStyle value="Size" />
<rollingStyle value="Composite" />
</appender>
<!-- END ROOT APPENDER -->
</log4net>

How do I make aspnet-request available in my NLog config?

I am trying to add the ip address to logging. I installed the NLog.Extended package and ensured that the NLog.Extended.dll is present in my bin next to the base NLog.dll. I have added the variable "${aspnet-request:serverVariable=remote_addr}" to my layout renderer. I get a generic error saying:
An error occurred creating the configuration section handler for nlog: Exception occurred when loading configuration from [my Web.config]
Here is my 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">
<extensions>
<add assembly="NLog.Extended" />
</extensions>
<targets async="true">
<target name="fileLog" xsi:type="File" fileName="${basedir}/logs/${shortdate}.log" layout="${aspnet-request:serverVariable=remote_addr} ${longdate} ${callsite} ${level} ${message} ${exception:format=ToString}" />
<target name="dbLog" xsi:type="Database" connectionStringName="db.data" commandText="insert into log ([Date], [Origin], [LogLevel], [Message], [Exception], [StackTrace]) values (#date, #origin, #logLevel, #message, #exception, #stackTrace)">
<parameter name="#date" layout="${date}"/>
<parameter name="#origin" layout="${callsite}"/>
<parameter name="#logLevel" layout="${level}"/>
<parameter name="#message" layout="${message}"/>
<parameter name="#exception" layout="${exception:format=Message,StackTrace}"/>
<parameter name="#stackTrace" layout="${stacktrace}"/>
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="dbLog" />
<logger name="*" minlevel="Trace" writeTo="fileLog" />
</rules>
</nlog>
When I remove the AspNetRequest variable it doesn't complain. I've tried replacing "remote_addr" with "remote_host" with no change. This project is using NLog 2.1.0. Any ideas on what I'm doing wrong here?
If you're using NLog 4.0 then you'll need NLog.Web to use the aspnet=* layout renderers.
It turned out that the NLog.Extended.dll(3.1.0.0) had to match the NLog.dll it was referencing. I upgraded the NLog.dll to 3.1.0.0 and everything is working now.
In my ASP.NET webapi 2.0 solution I had a separate projects for my API's and logging. Using nLog 4.4.12 installed through NuGet and all the helper packages (Config,Extended,Web,Schema) in the logging project.
The "aspnet-request" properties in my config would not work until I added the nLog references to my api project, even though "Copy Local" is set to true for all the nLog DLL's in the logging project. My nLog section from my web.config is below.
Hope this help someone,
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="db" xsi:type="Database" connectionStringName="NLogConn" commandType="StoredProcedure" commandText="[dbo].[NLog_AddEntry_p]">
<parameter name="#machineName" layout="${machinename}" />
<parameter name="#siteName" layout="${iis-site-name}" />
<parameter name="#logged" layout="${date}" />
<parameter name="#level" layout="${level}" />
<parameter name="#username" layout="${aspnet-user-identity}" />
<parameter name="#message" layout="${message}" />
<parameter name="#logger" layout="${logger}" />
<parameter name="#properties" layout="${all-event-properties:separator=|}" />
<parameter name="#serverName" layout="${aspnet-request:serverVariable=SERVER_NAME}" />
<parameter name="#port" layout="${aspnet-request:serverVariable=SERVER_PORT}" />
<parameter name="#url" layout="${aspnet-request:serverVariable=HTTP_URL}" />
<parameter name="#https" layout="${when:inner=1:when='${aspnet-request:serverVariable=HTTPS}' == 'on'}${when:inner=0:when='${aspnet-request:serverVariable=HTTPS}' != 'on'}" />
<parameter name="#serverAddress" layout="${aspnet-request:serverVariable=LOCAL_ADDR}" />
<parameter name="#remoteAddress" layout="${aspnet-request:serverVariable=REMOTE_ADDR}:${aspnet-request:serverVariable=REMOTE_PORT}" />
<parameter name="#callSite" layout="${callsite:fileName=true:includeSourcePath=false:skipFrames=1}" />
<parameter name="#exception" layout="${exception:tostring}" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Trace" writeTo="db" />
</rules>

Missing date and extension with log4net

I've just downloaded log4net via NuGet in VS2010. Log4net version 1.2.11.0.
My issue - I can't append the date or extension to the file name.
Here's my appender:
<appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
<file value="Logs\Error" />
<appendToFile value="true" />
<datePattern value=".yyyy-MM-dd.lo\g" />
<rollingStyle value="Date" />
<filter type="log4net.Filter.LevelRangeFilter">
<acceptOnMatch value="true" />
<levelMin value="ERROR" />
<levelMax value="FATAL" />
</filter>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern
value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n" />
</layout>
</appender>
The file is created in the correct directory (./Logs) with name "Error", no extension.
I've also tried datePattern ".yyyy-MM-dd.\l\o\g".
None of the other S.O. questions I've visited have helped. Any ideas?
Add the line
<staticLogFileName value="false" />
For details see
http://logging.apache.org/log4net/release/sdk/log4net.Appender.RollingFileAppender.StaticLogFileName.html

How to force nlog to throw an exception when the logging to database fails?

When I take down the database that backs nlog, nothing gets get logged and it seems NLog swallows the problem. Is there any way to configure it to raise and exception or at least to log in a text file that logging failed?
Here is what my configuration looks like:
<?xml version="1.0" ?>
<nlog autoReload="true" throwExceptions="true" internalLogFile="${basedir}/App_Data/nlog.txt" internalLogLevel="Debug"
internalLogToConsole="true">
<targets>
<!--Useful for debugging-->
<target name="consolelog" type="ColoredConsole"
layout="${date:format=HH\:mm\:ss}|${level}|${stacktrace}|${message}" />
<target name="databaselog" type="Database">
<dbProvider>System.Data.SqlClient</dbProvider>
<!-- database connection parameters -->
<!-- alternatively you could provide a single 'connectionstring' parameter -->
<connectionString>Data Source=.\SQLEXPRESSZ;Initial Catalog=aspnetdb;Integrated Security=SSPI</connectionString>
<commandText>
insert into NLog_Error ([time_stamp],[level],[host],[type],[source],[logger],[message],[stacktrace],[allxml]) values(#time_stamp,#level,#host,#type,#source,#logger,#message,#stacktrace,#allxml);
</commandText>
<parameter name="#time_stamp" layout="${utc_date}" />
<parameter name="#level" layout="${level}" />
<parameter name="#host" layout="${machinename}" />
<parameter name="#type" layout="${exception:format=type}" />
<parameter name="#source" layout="${callsite:className=true:fileName=false:includeSourcePath=false:methodName=false}" />
<parameter name="#logger" layout="${logger}" />
<parameter name="#message" layout="${message}" />
<parameter name="#stacktrace" layout="${exception:stacktrace}" />
<parameter name="#allxml" layout="${web_variables}" />
</target>
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="databaselog" />
</rules>
</nlog>
You can force Nlog to throw exception when sql server is not reached by following
<nlog throwExceptions="true">
... your nlog config
</nlog>
More info here,
http://nlog-project.org/2010/09/05/new-exception-handling-rules-in-nlog-2-0.html
It's a new feature in v2.0 so you need v2.0.
It will not work in earlier versions.
Also checkout following configuration info
https://github.com/NLog/NLog/wiki/Logging-Troubleshooting
which allows Nlog to log it's own exceptions to a specified file.
Does NLog.config have the property "Copy to Output Directory" set as "Copy always"?
I think you have wrong NLog.config file: you use elements instead of attributes within the target (documentation). Should be something like this:
<target
name="databaselog"
type="Database"
dbProvider="System.Data.SqlClient"
connectionString="Data Source=.\SQLEXPRESSZ;Initial Catalog=aspnetdb;Integrated Security=SSPI"
commandText="insert into NLog_Error ([time_stamp],[level],[host],[type],[source],[logger],[message],[stacktrace],[allxml]) values(#time_stamp,#level,#host,#type,#source,#logger,#message,#stacktrace,#allxml);">
<parameter name="#time_stamp" layout="${utc_date}" />
<parameter name="#level" layout="${level}" />
<parameter name="#host" layout="${machinename}" />
<parameter name="#type" layout="${exception:format=type}" />
<parameter name="#source" layout="${callsite:className=true:fileName=false:includeSourcePath=false:methodName=false}" />
<parameter name="#logger" layout="${logger}" />
<parameter name="#message" layout="${message}" />
<parameter name="#stacktrace" layout="${exception:stacktrace}" />
<parameter name="#allxml" layout="${web_variables}" />
</target>

Resources