Deletion of List Items While Looping

Peace be upon you,

There is a very popular logical bug when you are looping on a list to delete some of its items when a certain condition is satisfied, we would think of the algorithm to be like this:

for (int i = 0; i < list.Items.Count; i++)
{

if (condition)
list.Items.Delete(list.Items[i])

}

Unfortunately that won’t delete all the items satisfying the condition, because on deletion of an item we are changing the list structure by shifting the items to fill the gab caused by the item deleted while still advancing our index, so the item after the one we are deleting will be skipped.

Now how to solve this, it’s simple just don’t increase the index on item deletion like follows

for (int i = 0; i < list.Items.Count; i++)
{

if (condition)
{
list.Items.Delete(list.Items[i]);
i- -;
}

}

Happy Programming 🙂

Advertisements

Content Delivery Networks

Peace be Upon You,

Lets talk first about CDN or Content Delivery Network, Wikipedia says :

It is a system of computers containing copies of data, placed at various points in a network so as to maximize bandwidth for access to the data from clients throughout the network. A client accesses a copy of the data near to the client, as opposed to all clients accessing the same central server so as to avoid bottleneck near that server.

Content types include web objects, downloadable objects (media files, software, documents), applications, real time media streams, and other components of internet delivery (DNS, routes, and database queries).

So, that means a better performance for our web applications, but a question arises, how to use such service networks?

Well CDN is a service thats is served on free and commercial basis, one of the service providers is google who provides such service through the Google AJAX Libraries API.

Also Micorsoft has it’s own CDN network Microsoft Ajax Content Delivery Network.

Regards

How To Configure Log4Net Without Modifying Web.Config

Peace be upon you,

I was trying to configure Log4Net without making any modifications to the application web.config and going through the hassle of XML nodes, web.config backup specially if it was for a portal like sharepoint or something, etc.

I found a quick and easy way for such thing, I will illustrate it in steps:

1-      Add this line in your AssemblyInfo.cs file: [assembly: log4net.Config.XmlConfigurator(ConfigFile = “log4net.config”, Watch = true)]

2-      Create a file named by log4net.config and Add this example of configuration inside it:

<log4net>

<appender name=”FileAppender”>

<param name=”File” value=”C:\\tmp\\ log.txt” />

<param name=”AppendToFile” value=”true” />

<param name=”MaximumFileSize” value=”5MB” />

<layout type=”log4net.Layout.PatternLayout,log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821″>

<param name=”ConversionPattern” value=”%d [%t] %-5p %c [%x] %username – %m%n%newline%newline” />

</layout>

</appender>

<appender name=”EventLogAppender” type=”log4net.Appender.EventLogAppender” >

<applicationName value=”Your Application Name” />

<layout type=”log4net.Layout.PatternLayout”>

<conversionPattern value=”%date [%thread] %-5level %logger [%property{NDC}] – %message%newline” />

</layout>

</appender>

<root name=”Logger”>

<level value=”ALL” />

<appender-ref ref=”FileAppender” />

<appender-ref ref=”EventLogAppender” />

</root>

</log4net>

3-      put it beside web.config file of your web application.

Now you are logging in both, your text file and the event log of the system, Offcourse you can make further configurations for Log4Net but that’s not our topic and that’s just a sample config file.

I find this useful for SharePointers as well, such that you can create your wsp file, deploy it using Sharepoint Solution Installer or even your own MSI installer, and just putting your log4net file in the web application virtual folder and that’s it.

Hope you find this useful 🙂