Supertext Home
Chief of the System Blog

Archive for the 'Programming' Category


Using Log4Net for a Windows Console Application

Wednesday, February 11th, 2009

Most examples with Log4Net are with ASP.NET, but it works perfectly fine with Windows Form and Console Applications too.

What you need is an App.config file that you add to your project.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821" />
  </configSections>

    <!-- Log4net Logging Setup -->
  <log4net debug="false">
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="C:\\directory\\supertext_outsidetasks.log"/>
      <param name="AppendToFile" value="true"/>
      <maxSizeRollBackups value="10"/>
      <datePattern value="yyyy-MM-dd"/>
      <rollingStyle value="Date"/>
      <lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
      <layout type="log4net.Layout.PatternLayout">
        <header value="Date | Level | SessionID | Logger | Message | &#xA;"/>
        <param name="ConversionPattern" value="%date{ABSOLUTE}| %-5p | %-30logger| %m|%n"/>
      </layout>
    </appender>
    <root>
      <priority value="DEBUG"/>
      <appender-ref ref="RollingLogFileAppender"/>
    </root>
  </log4net>
</configuration>

Then initialize it in your main() function:

using log4net;
using log4net.Config;

static void Main(string[] args)
{
    log4net.Config.XmlConfigurator.Configure();

    //your code
}

And you can use it like in ASP.NET:

log.Info("Hello Logfile");

The only issue I had was that adding the configSection like this did not work:

<configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
</configSections>

I had to add the whole shebang with Version, etc. to it. Then it started working. Anyone else had that problem?

Anyway, are people still using Log4Net? There is not much activity on the project anymore and the last version is getting pretty old.


Change a CSS Control Adapter in Code

Tuesday, November 11th, 2008

I run into a situation where I needed different CSS Control Adapters for the ASP.NET Menu Control for differnent browsers, in my case a default one and one for the iPhone.

So my first idea was to just put multiple controlAdapters sections into a .browser file, but for whatever reason that did not work (I still think this should work somehow, if you have the answer, let me know). Then I was looking to just change the adapter in my code, but even Scott Guthrie said it’s not possible. Ok, time to call it a day and get a beer (or two).

But who cares if the Corporate Vice President in the Microsoft Developer Division says it’s not possible, lets go at it again. Eventually I found this post. If it is possible to add and remove Adapters at run time, it clearly has to be possible to change them, no? And indeed, it is possible:

protected override void OnPreInit(EventArgs e) { HttpContext.Current.Request.Browser.Adapters["System.Web.UI.WebControls.Menu, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"] = "Supertext.Adapters.iUiMenuAdapter"; base.OnPreInit(e); } protected override void OnUnload(EventArgs e) { base.OnUnload(e); HttpContext.Current.Request.Browser.Adapters["System.Web.UI.WebControls.Menu, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"] = "CSSFriendly.MenuAdapter"; }

Just add the above code to your aspx page and change the key/value pair appropriately. You can iterate through the Adapters list to get a better idea what is in there and how to replace it.

Now, this is not exactly a schoolbook code sample, but it works and it’s just intended to give you kick in the right direction. In that sense, please no of these “this is unmaintainable code” comments. Thanks!


Find checked Radio Button in ASP.NET

Friday, February 22nd, 2008

If you need a little more layout freedom than the RadioButtonList gives you in ASP.NET you have to resort to the normal RadioButton. Unfortunately there is no easy way to afterwards figure out which button has been checked.

Here is a simple recursive Script in C#:

/// <summary> /// Searches recursivly through the controls tree for the /// Checked RadioButton. /// </summary> /// <param name="parent"> /// Control that contains the RadioButtons, can be the Page object. /// </param> /// <param name="groupName"> /// Name of the RadioButton Group /// </param> /// <returns>The RadioButton, null if it is not found</returns> public RadioButton GetCheckedRadioButton( Control parent, string groupName) { foreach (Control ctrl in parent.Controls) { if ((ctrl is RadioButton) && ((RadioButton)ctrl).GroupName.Equals(groupName) && ((RadioButton)ctrl).Checked == true) { return (RadioButton)ctrl; } else if (ctrl.Controls.Count > 0) { Control fromChild; fromChild = GetCheckedRadioButton( ctrl, groupName); if (fromChild != null) return (RadioButton)fromChild; } } return null; }//end GetCheckedRadioButton

A function in VB would be very similar. Good luck!


File Upload with ASP.NET

Tuesday, January 29th, 2008

Jon Galloway already had some interesting thoughts about uploading large files with ASP.NET. Unfortunately he didn’t really advise on one good solution.
So I decided (or had) to take a look myself. What I needed is an upload button with a progress bar that works on a Mac and on a PC with a reasonable recent browser.

And here are the results:


Convincing Solutions: (They normally worked on IE, but not necessarily on Mac)

KUpload
A bit a confusing product palette and pricing, but looks like it starts at $79 plus $19 for the progress bar. Or $499 with Source Code. Demo works fine. Web-Site looks a little unprofessional.

DEXTUpload .NET
Component seems to work fine, nice progress bar and decent pricing for $338. Homepage works a bit strange, but info is there. No success on Mac.

SlickUpload
Nice simple online Demo and webpage. Starts at $149. Progress Bar updates nicely with infos about uploaded kBytes and time remaining. Forum has posts and lots of views. Last update was on the 06/24/2007. Works on Mac Safari, FF and on Windows.

Dart File Upload for ASP.NET
Looks professional and the online Demo works fine. Nice progress bar with additional information. Skinning possibilities and a special File Dialog button, so you don’t see the “Browse” button. Starts at $249.
Progress Bar does not really move on Safari or FF on Mac.

Essential Objects Ajax Uploaded
Another nice component with a good online Demo. Pricing starts at $149. Multiple Postback options and different skinning possibilities. Works fine in all tested browsers.

Telerik RadUpload Promoetheus
Telerik is a big provider of ASP.NET components with a pretty good reputation as far as I know. Their Upload component seems to be one of the more powerful on this list. The individual control starts at $249 and the whole suit is $799.
Based on this info, I decided to give it a try. Unfortunately, it does not currently work in Integrated Mode with IIS 7. Neither does the Progress Bar work in Safari 3 on a Mac 10.5.1 (Leopard). Very disappointing.

 

Not so convincing Solutions: (Demo’s did not work on my Vista IE7)

UpFilesBE
Look more like a hobbyist component, also, no updates since a while, no forum, etc. On the other hand, it’s only $49.

Update: Original Link does not work anymore: http://www.datanyms.com, but got a comment with a new Link to expanData AND a new price, now its $999. I tested it again. Looks like the demo is still the old one. It works fine in IE, but still not in Safari.

AsyncFileUpload
The only one that even mentions Ajax Panel compatibility, other than that, not much there. No Progress Bar either. $69 is ok. Last update was on May 2007.

FileUp
For $499 from SoftArtisans. There is no Progress Bar, at least in the Online Demo. Rather weak online presentation, but seems to be one of the players in the Upload Component Area.

PowUpload
With $199 in the middle field if it comes to pricing. Demo works fine, but the Progress Bar clearly reloads after each update and runs in a Popup. There are better solutions out there.

ABCUpload .Net
One of the more expensive tools for $899. The Upload happens in Popup Window, which I don’t really like too much, also the price is pretty high and I’ve never heard from this company before.

Mediachase FileUploader.NET
Starts at $310 and goes up to $3625 for a Corporate License. Progress Bar is in a Popup, but did not work in my test.

csNetUpload
Only $50, but no online demo or forum available, so I didn’t test this any further.

UploadProgressTracker
Again only $50, but looks like there has been no update since 2006, not really a good candidate either. Demo works fine though.

JUpload, Active Upload, Upload Friendly need a Java Applet to work, which is not something we want to do. So I didn’t look at them any further.

 

Open Source

NeatUpload
Looks like a pretty cool Open Source Component. The Progress Bar is in an IFrame and seems to do its job, but the update steps are not very fine grained. Also supports Mono!
Unfortunately it also does not work with Integrated Mode in IIS7. Please help voting so that Microsoft fixes this issue.
Passed the test in all my browsers (Mac and PC). Forum is full and recent, support seems to be available.

SWFUpload
The only Flash based component in this test. Was my longtime favorite and still is to a certain degree. We used this for Supertext for a while now. Really like the architecture and the features, unfortunately we had many users for which it didn’t work. Would be my first choice for an Intranet solution or if you have a limited user base.

FileUploadAjax
Didn’t see much Ajax or even a Progress Bar on my quick test, but does have decent amount of online examples.
Forum has tons of posts and latest version is from the 7/27/2007.

SharpUpload
Looks like this is rather a helper component to handle uploaded files, than something like the other tool that come with progress bars. etc.

Multiple File Upload With Progress Bar Using Flash and ASP.NET
From the Codeproject Website. Got really good reviews, but didn’t get around to try it out yet.

Summary
HORRIBLE! More than half of the products don’t really work, most of them don’t do anything on a Mac. Not sure why this has to be such an issue. Is this only an IIS/ASP issue?
The only 3 Components that really worked in our quick tests were SlickUpload, Essential Objects and NeatUpload. We kinda liked SlickUpload, but decided to give NeatUpload a try. Will keep you posted about the results.

Would really love to hear what experiences that other people had?



DataNavigateUrlFormatString with multiple fields

Wednesday, January 16th, 2008

If you have an ASP.NET GridView with a HyperLinkfield you can not only pass just one DataField, but actually multiple in a very simple way.

The Property is already called DataNavigateUrlFields (with an s, hint, hint).

<asp:HyperLinkField DataNavigateUrlFields="iddocument,documentName" DataNavigateUrlFormatString="../DocumentDownload/{0}/{1}" DataTextField="documentName" HeaderText="Document" SortExpression="documentName" />

Not much more to it. Got the hint from AzamSharp. Alternatively you can always use a TemplateField, which gives you more flexibility.

<asp:TemplateField> <ItemTemplate> <asp:HyperLink ID="lnkShowFile1" runat="server" NavigateUrl='<%# "../DocumentDownload/" + Eval("iddocument") + "/" + Eval("documentName") %>' > <%# Eval("documentName") %> </asp:HyperLink> </ItemTemplate> </asp:TemplateField>


Very simple MySQL backup via FTP script

Friday, November 9th, 2007

There are tons of pretty enhanced scripts out there to make backup and also to copy them to FTP sites, but what I needed was just something really simple to have my daily backup saved somewhere.

The script below will dump your DB into the backup.sql file and copy it to an FTP server of your choice into the root directory of that user. Can’t be any simpler.

Here it is:

The BackupAndFTP.cmd file:

mysqldump -uuserName -ppassword DBName > backup.sql ftp -s:ftpcommands.txt ftp.hostname.com

The ftpcommands.txt file:

username password binary put backup.sql bye

Just put both code blocks into an individual file, replace the usernames and passwords (first for MySQL and then for the FTP access), replace DBName with the name of the database you wanna save and that’s it.

  • Topics
  • Archive
  • Subscribe
  • Facebook
  • Twitter