Writing assembly version on splash screen - wpf

I followed Kent Boogaart's excellent article on how to add dynamic content to the splash screen. Things work in the sense that I can see version number on the splash screen now. The problem however is that the article uses $(Version) property, which in my case is 1.0.0.0.
I am using shared AssemblyInfo.vb and need to get next major/minor/build/revision numbers from this file to draw them on the splash screen. The shared AssemblyInfo file contains a version number like 2.5.*. I need the actual number that will be generated by MSBuild (2.5.abc.xyz).
I thought about using Reflection inside the UsingTask to get version number from the assembly, but since this task is run before the build process, the assembly doesn't yet have the version number that will be generated in the following build. Moreover the assembly simply may not exist (e.g. a Clean command before build) at that point.
I installed MSBuild Community Tasks but cannot see anything except SvnVersion task.
Can someone help me with sending the to-be-generated version number to my UsingTask?

Finally! For anyone else trying to do the same, here are the steps, assuming that you're using shared AssemblyInfo that resides in solution directory (it can work for default AssemblyInfo too, but you'll need to adjust paths and filenames accordingly):
Download and install MSBuild Community Tasks on your machine.
Add a targets file (a simple XML file with .targets extension) to your project.
Add UsingTask presented in Kent Boogaart's article I linked in the question to this file. This will perform actual version writing on the splash image.
Use <Version>, <FileUpdate> and <AddTextToImage> tasks (first two are available in MSBuild, third one is from the UsingTask we added in step 3) to write new version number to your shared AssemblyInfo file and splash image.
The final .targets file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="AddTextToImage" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<InputPath ParameterType="System.String" Required="true" />
<OutputPath ParameterType="System.String" Required="true" />
<TopMiddlePoint ParameterType="System.String" Required="true" />
<Text1 ParameterType="System.String" Required="true" />
<Text2 ParameterType="System.String" Required="false" />
<Text3 ParameterType="System.String" Required="false" />
</ParameterGroup>
<Task>
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System.Xaml" />
<Using Namespace="System" />
<Using Namespace="System.Globalization" />
<Using Namespace="System.IO" />
<Using Namespace="System.Windows" />
<Using Namespace="System.Windows.Media" />
<Using Namespace="System.Windows.Media.Imaging" />
<Code Type="Fragment" Language="cs">
<![CDATA[
var originalImageSource = BitmapFrame.Create(new Uri(InputPath));
var visual = new DrawingVisual();
using (var drawingContext = visual.RenderOpen())
{
drawingContext.DrawImage(originalImageSource, new Rect(0, 0, originalImageSource.PixelWidth, originalImageSource.PixelHeight));
var typeFace = new Typeface(new FontFamily("Arial"), FontStyles.Normal, FontWeights.Bold, FontStretches.Normal);
var formattedText = new FormattedText(Text1, System.Globalization.CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeFace, 18, Brushes.Red);
var topMiddlePoint = Point.Parse(TopMiddlePoint);
var point = new Point(topMiddlePoint.X - (formattedText.Width / 2), topMiddlePoint.Y);
drawingContext.DrawText(formattedText, point);
if(!string.IsNullOrEmpty(Text2))
{
formattedText = new FormattedText(Text2, System.Globalization.CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeFace, 18, Brushes.Red);
topMiddlePoint.Y += formattedText.Height + 5;
point = new Point(topMiddlePoint.X - (formattedText.Width / 2), topMiddlePoint.Y);
drawingContext.DrawText(formattedText, point);
}
if(!string.IsNullOrEmpty(Text3))
{
formattedText = new FormattedText(Text3, System.Globalization.CultureInfo.InvariantCulture, FlowDirection.LeftToRight, typeFace, 18, Brushes.Red);
topMiddlePoint.Y += formattedText.Height + 5;
point = new Point(topMiddlePoint.X - (formattedText.Width / 2), topMiddlePoint.Y);
drawingContext.DrawText(formattedText, point);
}
drawingContext.Close();
}
var renderTargetBitmap = new RenderTargetBitmap(originalImageSource.PixelWidth, originalImageSource.PixelHeight, originalImageSource.DpiX, originalImageSource.DpiY, PixelFormats.Pbgra32);
renderTargetBitmap.Render(visual);
var bitmapFrame = BitmapFrame.Create(renderTargetBitmap);
BitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(bitmapFrame);
using (var stream = File.OpenWrite(OutputPath))
{
encoder.Save(stream);
stream.Close();
}
]]>
</Code>
</Task>
</UsingTask>
<PropertyGroup>
<MajorVersion>2</MajorVersion>
<MinorVersion>5</MinorVersion>
</PropertyGroup>
<Target Name="BeforeBuild">
<Version BuildType="Automatic" RevisionType="Automatic" Major="$(MajorVersion)" Minor="$(MinorVersion)">
<Output TaskParameter="Major" PropertyName="Major" />
<Output TaskParameter="Minor" PropertyName="Minor" />
<Output TaskParameter="Build" PropertyName="Build" />
<Output TaskParameter="Revision" PropertyName="Revision" />
</Version>
<FileUpdate Files="$(SolutionDir)SharedAssemblyInfo.vb"
Regex="Assembly: AssemblyVersion\("\d+\.\d+((\.\*)|(\.\d+\.\d+))?"\)"
ReplacementText="Assembly: AssemblyVersion("$(Major).$(Minor).$(Build).$(Revision)")" />
<FileUpdate Files="$(SolutionDir)SharedAssemblyInfo.vb"
Regex="Assembly: AssemblyFileVersion\("\d+\.\d+((\.\*)|(\.\d+\.\d+))?"\)"
ReplacementText="Assembly: AssemblyFileVersion("$(Major).$(Minor).$(Build).$(Revision)")" />
<SvnVersion LocalPath=".">
<Output TaskParameter="Revision" PropertyName="SvnRevision" />
</SvnVersion>
<AddTextToImage InputPath="$(ProjectDir)Resources\Splash.png"
OutputPath="$(ProjectDir)Resources\SplashWithVersion.png"
TopMiddlePoint="250,150"
Text1="$(Major).$(Minor).$(Build).$(Revision)"
Text2="SVN Version: $(SvnRevision)" />
<Message Text="Updated version number on splash screen to: $(Major).$(Minor).$(Build).$(Revision)" Importance="high"/>
</Target>
</Project>
This will update your AssemblyInfo and the output image. Note that you must mark your output image as SplashScreen (as Build Action) in Visual Studio. Also note that I'm writing both assembly version as well as SVN Revision number on the splash screen. You may want to adjust yours according to your needs.

Related

Where are the settings saved in a .NET 5 WinForms app?

In a .NET Framework WinForms project, there was an App.config file in the project, which was an XML file that contained a configSection that would reference a class in System.Configuration, and a section for the userSettings themselves, like so:
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561944e089">
<section name="MyAppName.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561944e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<MyAppName.Properties.Settings>
<setting name="Test" serializeAs="String">
<value>Some Value</value>
</setting>
</MyAppName.Properties.Settings>
</userSettings>
And this created a file in the build folder with the app name plus .exe.config, as in MyAppName.exe.config.
But when I create a new WinForms project using .NET:
There is no App.config in the solution. I can edit the settings using the project properties:
And I can access these values, and update them using the same Properties object and methods:
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
textBox1.Text = Properties.Settings.Default.Test;
}
private void button1_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Test = textBox1.Text;
Properties.Settings.Default.Save();
}
}
}
And everything seems to work, but when I examine the bin folder, there is no file that I can see for where the values are actually stored.
Where is .NET 5 storing the saved application settings if not in a file in the same folder as the application's exe?
User settings are stored in user.config file in the following path:
%userprofile%\appdata\local\<Application name>\<Application uri hash>\<Application version>
Application settings file are not created by default (unexpectedly), however if you create them manually beside the dll/exe file of your application, the configuration system respect to it. The file name should be <Application name>.dll.config. Pay attention to the file extension which is .dll.config.
You may want to take a look at the source code of the following classes:
LocalFileSettingsProvider (The default setting provider)
ClientSettingsStore
ConfigurationManagerInternal
ClientConfigurationPaths
At the time of writing this answer Application Settings for Windows Forms still doesn't have any entry for .NET 5 and redirects to 4.x documentations.
First of all, this is a known (to .NET team) issue: https://github.com/dotnet/project-system/issues/7772.
Secondly the issue and the solution are pretty much described in your question:
(before) ..there was an App.config file in the project,..
(now) There is no App.config in the solution...
Add the missing app.config and everything will work just like it did before.

Why cannot write log to files when WPF application publishing as a single file

I use Microsoft.Extensions.Hosting and NLog.Web.AspNetCore in WPF. The application run correctly with Debug and Release mode, But when I publish the app as a single file, I found File target does not work when fileName using relative path.
NLog version: 4.6.8
Platform: .NET Core 3
NLog config
<nlog>
<targets>
<default-wrapper xsi:type="BufferingWrapper" bufferSize="100"/>
<target xsi:type="File" name="file" fileName="logs/${level}-${shortdate}.log" encoding="utf-8"
layout="${longdate}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="file" final="true"/>
</rules>
</nlog>
I use AddNLog to apply this configuration:
public App()
{
_host = new HostBuilder()
.ConfigureLogging(logBuilder =>
{
logBuilder.ClearProviders()
.SetMinimumLevel(LogLevel.Debug)
.AddNLog("NLog.config");
})
.ConfigureServices((hostContext, services) =>
{
//...
}).Build();
}
Show the MainWindow when application startup:
private void Application_Startup(object sender, StartupEventArgs e)
{
using var serviceScope = _host.Services.CreateScope();
var serviceProvider = serviceScope.ServiceProvider;
_logger = serviceProvider.GetRequiredService<ILogger<App>>();
SetupExceptionHandling();
MainWindow mainWindow = serviceProvider.GetRequiredService<MainWindow>();
mainWindow.Show();
_logger.LogInformation($"Application startup at {DateTime.Now} successfully");
}
Publishing as a single file and run it, the log of successful startup is not written to the file, But when i change fileName to an absolute path like /logs/${level}-${shortdate}.log or ${level}-${shortdate}.log, the log can be written.
I try to configure it in code:
var config = new LoggingConfiguration();
var file = new FileTarget("file")
{
FileName = "logs/${shortdate}-${level}.log",
Layout = "${longdate}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}"
};
config.AddRule(LogLevel.Info, LogLevel.Fatal, new BufferingTargetWrapper(file));
return config;
But the result is still the same.
Am I writing something wrong? thanks for your help.
NLog will automatically prefix relative fileName-path with the ${basedir}-layout. See also https://github.com/nlog/nlog/wiki/Basedir-Layout-Renderer
Sadly enough Microsoft decided not to fix AppDomain.BaseDirectory when doing Single File Publish in NetCore 3.1
https://github.com/dotnet/aspnetcore/issues/12621
https://github.com/dotnet/core-setup/issues/7491
The work-around is to explictly specify ${basedir:fixTempDir=true}:
<nlog>
<targets>
<default-wrapper xsi:type="BufferingWrapper" bufferSize="100"/>
<target xsi:type="File" name="file" fileName="${basedir:fixtempdir=true}/logs/${level}-${shortdate}.log" encoding="utf-8"
layout="${longdate}|${uppercase:${level}}|${logger}|${message} ${exception:format=tostring}" />
</targets>
<rules>
<logger name="*" minlevel="Info" writeTo="file" final="true"/>
</rules>
</nlog>
Hopefully Microsoft will fix the illusion with NetCore5

log4net log file not being written when the app is deployed

I have a WPF app that uses log4net. When I run it in Visual Studio, the log file is created in the Debug or Release folder as expected.
However, when I create an installer and run the installed app, the log file is not created. I added the following lines to the code...
string logFilePath = ((Hierarchy)LogManager.GetRepository())
.Root.Appenders.OfType<FileAppender>()
.FirstOrDefault()?.File;
using (StreamWriter sw = new StreamWriter(#"d:\log.log")) {
sw.WriteLine("Log file: " + logFilePath);
}
...to enable me to check that the log file was being written in the location I expected. It showed me that the log file was supposed to be written to C:\Program Files (x86)\Physio Diary\PhysioDiaryClient.log which is what I expected.
However, the file doesn't exist. Any idea why?
Here is the top of the App.config file...
<?xml version="1.0"
encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net"
type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" />
</configSections>
<log4net>
<appender name="RollingFileAppender"
type="log4net.Appender.RollingFileAppender">
<param name="File"
value="PhysioDiaryClient.log" />
<appendToFile value="true" />
<rollingStyle value="Size" />
<maxSizeRollBackups value="2" />
<maximumFileSize value="1MB" />
<staticLogFileName value="true" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%date [%thread] %-7level %logger - %message%newline%exception" />
</layout>
</appender>
<root>
<level value="DEBUG" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
The bottom of the file looks like this...
<startup>
<supportedRuntime version="v4.0"
sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
The bits in between are all to do with the WCF services that the app uses.
Anyone any ideas?
Edit: As a test, I tried hard-coding the log file path in App.config to my D: drive (so it's hard-coded, and no question of a permissions issue), but the file still wasn't created.
Thanks to #dymanoid for pointing me in the right direction. The log4net docs are a bit weak in this area, but I found this answer that pointed out that you can use normal environment variables in the config file.
With the aid of this list of environment variables, I ended up with the following...
<param name="File"
value="${LOCALAPPDATA}\Physio Diary\PhysioDiaryClient.log" />
This correctly write the file to C:\Users\MyUsername\AppData\Local\Physio Diary\PhysioDiaryClient.log
Hope this helps someone.

Can't Compile using SqlDataConnection in F# on Mono in Linux

I am using mono on Linux, making an F# code to access an SQL Server Database. I am using SqlDataConnection.
My code is the following:
namespace AggregatorService
open FSharp.Data.TypeProviders
open System.Data
open System.Data.Linq
module DataBaseProvider =
type databaseConn = SqlDataConnection<ConnectionString = "Data Source=10.0.40.11;Initial Catalog=master;Persist Security Info=True;User ID=sa;Password=xxxxx">
let getDataContext() =
let dbConn = databaseConn.GetDataContext();
dbConn
I can't compile this code. I am getting this annoying error:
The type provider 'FSharp.Data.TypeProvider.DesignTime.DataProviders' reported an error reading schema. No access to the given key (FIS3033)
My package.config file is:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Deedle" version="1.2.5" targetFramework="net40" />
<package id="FSharp.Core" version="4.0.0.1" targetFramework="net40" />
<package id="FSharp.Data" version="2.3.2" targetFramework="net40" />
<package id="FSharp.Data.SqlClient" version="1.8.2" targetFramework="net40" />
<package id="FSharp.Data.TypeProviders" version="5.0.0.2" targetFramework="net40" />
<package id="NUnit" version="2.6.4" targetFramework="net40" />
<package id="SQLProvider" version="1.0.22" targetFramework="net40" />
</packages>
A C# program is connecting from the same computer, so, I think the problem is not with mono configurations or to be in a Linux machine.
Anyone has a solution or a Tip on how to solve that?
Unfortunatelly it looks mono is not able to deal this package as well I wish. Searching I found there is a bug from two years ago, but I don't find any solution yet.
What I am doing to leave that is using FSharp.Data.Sql instead, then, I use SqlDataProvider instead of SqlDataConnection, ow it looks:
namespace AggregatorService
open FSharp.Data.Sql
open Deedle
open System.Linq
module DatabaseService =
[<Literal>]
let connectionString = "Data Source=10.0.40.11;Initial Catalog=nextel_ericsson_umts_brasil;Persist Security Info=True;User ID=sa;Password=****";
type bd = SqlDataProvider<
ConnectionString = connectionString,
DatabaseVendor = Common.DatabaseProviderTypes.MSSQLSERVER >
type Database() =
static member contextDbo() =
bd.GetDataContext().Dbo
static member acAgregations() =
Database.contextDbo().AcAgregations |> Frame.ofRecords
static member acBusyHourDefinition() =
Database.contextDbo().AcBusyHourDefinition
|> Frame.ofRecords
//|> Frame.getCols["time_agregation_type", "destination_table", "reference_table", "alternative_reference_table_scan", "formula"]
static member acBusyHourDefinitionFilterByTimeAgregationTipe(value:int) =
Database.acBusyHourDefinition()
|> Frame.indexRowsInt "tome_agregation_type"
|> Frame.getRows
|> Frame.filterRowValues( fun row -> row.GetAs<string list>)
As I can see, that works the same way. I don't know if there is some difference expected, but it's dealing with my needs until now.

write log to file instead of console in postsharp

I know this question may be ridiculous but I could not find the answer. The Post sharp writes the logs in console by System.Diagnostics but I need to write the logs in a separate file. Is there any way to do so?
Thanks in advance
I found also I can do this in the app.config as the following:
<system.diagnostics>
<trace autoflush="true" indentsize="4">
<listeners>
<add name="myListener" type="System.Diagnostics.TextWriterTraceListener" initializeData="C:\\log.txt" />
<remove name="Default" />
</listeners>
</trace>
</system.diagnostics>
You need to use System.Diagnostics.Trace.Listeners property to register your own listener. You would need code like this in your app's entry point:
using (StreamWriter sw = new StreamWriter("file.txt"))
using (TextWriterTraceListener tl = new TextWriterTraceListener(sw))
{
Trace.Listeners.Add(tl);
try
{
// execute your program here
}
finally
{
Trace.Listeners.Remove(tl);
}
}

Resources