Monday, March 26, 2007

The coolest log parser

Once again Microsoft has come up with an awesome and very smart utility called Log Parser. It uses SQL-like syntax to allow you to query many kinds of sources. Example sources are web logs from IIS, EventLog in Windows, Active Directory, LDAP, etc. Here is how I got hits on a specific page in my application by month: LogParser "SELECT TO_STRING(date, 'yyyy-MM') as MONTH, COUNT(*) AS Hits FROM <FrontLine> where cs-uri-stem = '/KnowledgeBase.aspx' GROUP BY TO_STRING(date, 'yyyy-MM'), cs-uri-stem ORDER BY TO_STRING(date, 'yyyy-MM') DESC" In case that wasn't cool enough, you can output results to a chart (file), Database, Excel, etc. Here is where you can get the latest binary from: http://www.microsoft.com/downloads/details.aspx?FamilyID=890cd06b-abf8-4c25-91b2-f8d975cf8c07&displaylang=en Here is a good example for web logs usage: http://geekswithblogs.net/woodenshoe/archive/2005/09/17/54194.aspx Here is how to use it from C#: http://www.codeproject.com/csharp/SimpleLogParse.asp This seems to be a good tutorial: http://www.microsoft.com/technet/community/columns/scripts/sg0105.mspx

Wednesday, March 14, 2007

Waiting in SQL 2005

Have you ever needed to wait in a stored procedure? Well here is how. Declare @Counter as int

Set @Counter = 0 while @Counter < 3 BEGIN print @Counter Set @Counter = @Counter + 1 WAITFOR DELAY '00:00:02' -- waits for 2 seconds before next line is executed. END

Thursday, March 8, 2007

ASP.Net Custom Server Control Lessons

When writing a custom server control in ASP.Net it is pretty straight forward. It is common to put the output of the control right in the Render() method. Be aware this is very late in the control and page lifecycle. What that means if you try to modify the value of another control on the page it will appear to work in code, but the changes will not be shown in the browser. The reason is that the page is being rendered, it is too late to change .net server control values. So, it is easy to do put the "render" code in OnLoad(). Here is an example.

protected override void OnLoad(EventArgs e) { // do "rendering here" if you need to change values on the page i.e. raising an event ControlOutput = "<span>Hi</span>"; } protected override void Render(HtmlTextWriter output) { output.Write(ControlOutput); }

If you want to reproduce something link a LinkButton you will need to implement IPostBackEventHandler. This works in conjunction with __doPostBack() method.

Page.ClientScript.GetPostBackEventReference(this, "MyActionNameHere"). To learn more about this, check out the ASP.Net QuickStart

Wednesday, February 28, 2007

Events not showing up in EventLog

At some point ASP.Net web applications stopped being able to log to the event log. There are a few ways this can be fixed. Follow one of the suggestions in this tech note from Microsoft: http://support.microsoft.com/kb/329291 Give Network Service or ASP.Net permissions using regedit.exe (not regedit32 as it is broken). Give permissions to HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\EventLog and right-click. Choose Security Permissions. Give the account that your application runs under Full Control of the key and all subkeys
  • If deploying in IIS 5 (for example: Windows XP Professional) Give the ASPNET account Full Control.
  • If deploying in IIS 6 (for example: Windows Server 2003) Give the NETWORK SERVICE account Full Control. NOTE: If running your application pool under a different user than NETWORK SERVICE, then give that user account Full Control instead of NETWORK SERVICE. This is the user that the process will run under.
Warning: Option 2 doesn't seem to help if application is trying to write to Security log. If you create the key manually as Option 1 suggests you will need add another registry key as explained in the following entry I wrote. http://justgeeks.blogspot.com/2007/10/aspnet-and-eventlog-event-id-issues_1860.html

Tuesday, February 27, 2007

Java Application Server start and stop manually

Starting the Java Application Server that comes with Java Studio Enterprise 8 by running the start-domain is not recommended due to parameters that are expected by the batch file. Consequently the batch file hangs. To properly start the Java Application Server, open a command prompt and type: Asadmin start-domain

To properly stop the Java Application Server, open a command prompt and type: Asadmin stop-domain

Thursday, February 22, 2007

.Net Caching Rocks!

OK, I can't believe how cool .Net's built in Caching is. Here is the scenario. I need to query an Oracle database that I don't have anything but select permissions on. So, executing stored procedures and definitely can't create a stored procedure. So, what am I to do? Embed the query in code? That may be fine for small queries, but what if you query is quite long and complex. Sure, technically it can be put in the code, but who wants to maintain it. My solution was to put it in a file that is read from the file system at runtime. The problem is that the file system is hit everytime. So, I decided to cache it using the caching built into .Net. Below is my method that does this. The part that is so cool is that I can set a dependency for when an item in the cache is removed. As it turns out, one of the dependencies is a filename and path. If the file changes the cache is automatically notified. That is magic! :) using System.Web.Caching; .... public string LoadSQL(string filename) { object sqlObj = HttpContext.Current.Cache.Get(filename); string sql = string.Empty; if (sqlObj == null) { string path = HttpContext.Current.Request.PhysicalApplicationPath; string filenameAndPath = string.Format(@"{0}SQL\{1}", path, filename); using (StreamReader reader = new StreamReader(filenameAndPath, true)) { sql = reader.ReadToEnd(); System.Web.Caching.CacheDependency dependency = new CacheDependency(filenameAndPath); HttpContext.Current.Cache.Add(filename, sql, dependency, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.High, null); return sql; } } else { sql = sqlObj as string; } return sql; }

Wednesday, February 21, 2007

SQL 2005 ROW_NUMBER()

SQL Server 2005 no makes it easy to do paging of results and every n rows queries now that the row number can be obtained without using a temp table.

The new functionality is given by ROW_NUMBER(). Here is an example that returns every other row regardless if rows have been deleted, missing pks in the sequence, etc:

select Employee_number, myrownum from

(

select

Employee_number,

row_number() over (order by sc.FIRST_NAME asc) as myrownum

from

person

) as temp_person

where

myrownum % 2 = 0

order by myrownum asc

NOTE: the LAST line it is very important if you want to see the rows returned in the order of the row numbers.