Tahir Hassan's Blog

My Technical Notes

Tuesday, 22 July 2014

Writing a simple LogFactory using F# (with NLog)

Using NLog in F#

The following code instantiates a new Logger object to log to a file "myLogFile.txt" into the temp directory:

Friday, 18 July 2014

Strongly-typed Html.ActionLink

Html.ActionLink is fraught with the possibility that either the controller or action or parameter name(s) have been changed. As a way of making sure that these are always valid, we can use a strongly typed lambda expression.

We first define an ActionLink method which takes in the linkText (a string) and a lambda expression representing the controller, action and parameter(s):

Given a test action TestAction defined in the HomeController class:

We can add to our view:

Wednesday, 2 July 2014

Printing Log Messages with Time in SQL Server

Within stored procedures, PRINT statements appear at the very end when the stored procedure has fully executed. This makes it an unlikely candidate for printing out messages of far the stored procedure has progressed - what we'd rather want is immediate printing of the message.

To this end, I wrote a little stored procedure LogMessage that takes a 'string' as an argument and then prints this together with the time:

To use it: which prints:

Thursday, 19 June 2014

Showing Query Plan Text in SQL Server Managment Studio

Execute the statement:

Thereafter you can always set it to OFF.

Also select the Results to Text option, so it doesn't appear in a table.

See Displaying Execution Plans for more information.

Wednesday, 18 June 2014

Adding CSS Dynamically to a page via Javascript/JQuery

Adding CSS dynamically onto a page can be useful if you are making a simple GreaseMonkey script which changes the css styles to make the text more readable. First define a simple function addCss:

If you do not have JQuery on the page, you can always use vanilla js: Finally we can add any css we want to:

Wednesday, 11 June 2014

Embedding F# In PowerShell

Following on from Doug Finke's ‘Embedding’ F# in PowerShell article, I wanted to load F# scripts within PowerShell.

Later, after a bit of Google-ing, I worked out that you don't need to use the fsc.exe compiler, because we can instead use Add-Type. However we need a "CodeDom" for the F#. I ended up installing The Old F# "PowerPack", which includes one in its FSharp.Compiler.CodeDom.dll DLL file.

Here is the definition of my Add-FSharpType CmdLet:

Give either of these two parameters:

  • Path - the path to the F# script file.
  • TypeDefinition - a string containing the F# code.

To invoke the Fibonacci code: