Posts

Showing posts from 2011

Migrating from Subversion to Mercurial

This is an alternative way of migrating from SVN to Mercurial using HgSubversion as oppose to using convert extension. It allows you to push changes from local Mercurial repository to both Subversion and Mercurial during the transition period.Install HgSubversionhg clone http://bitbucket.org/durin42/hgsubversion/ <local-path>\hgsubversionAdd HgSubversion extension to your main hgrc file[extensions]
hgsubversion = <local-path>\hgsubversion\hgsubversionClone existing subversion repositoryhg clone http://<username>@<svn-project-path> <local-project-folder>Edit project .hgrc file (/hg/.hgrc)[paths]
default = http://<username>@<hg-remote-project-path>
svn = http://<username>@<svn-project-path>Create new mercurial project in your remote Hg repository (see Rhodecode)Pushing the project to remote mercurial repositoryhg pushPushing to subversionhg push svnIf you’re using mercurial_keyring extension to store Hg passwords and you…

Save window size & position in WPF

* Right Click on Settings.settings and Open With XML (Text) Editor* Add Top, Left, Height, Width & WindowState settings<Settings><SettingName="Top"Type="System.Double"Scope="User"><ValueProfile="(Default)">50</Value></Setting><SettingName="Left"Type="System.Double"Scope="User"><ValueProfile="(Default)">50</Value></Setting><SettingName="Height"Type="System.Double"Scope="User"><ValueProfile="(Default)">800</Value></Setting><SettingName="Width"Type="System.Double"Scope="User"><ValueProfile="(Default)">1200</Value></Setting><SettingName="WindowState"Type="System.Windows.WindowState"Scope="User"><ValueProfile="(Default)">Normal</Value></Setting></Settings>* Ad…

RhodeCode open source HG (Mercurial) server

Download RhodeCode
* http://rhodecode.org/Prerequisites
* Install Python 2.5 or later http://www.python.org/download/
* Add to PATH \Python27 and \Python27\Scripts folders
* Install SetupTools http://pypi.python.org/packages/2.7/s/setuptools/Create \RhodeCode folderInstall RhodeCode
* easy_install rhodecodeConfigure RhodeCode
* paster make-config RhodeCode production.ini
* paster setup-app production.iniRun RhodeCode
* paster serve production.iniServer is now running here http://localhost:8000To change default host:port edit production.ini fileMore info about installation and setup
http://packages.python.org/RhodeCode/installation.html
http://packages.python.org/RhodeCode/setup.html

C# Deep & Shallow Copy/Clone

class Program { staticvoid Main(string[] args) { var parent1 = new Parent() {Id = 1}; parent1.Children = new List<Child>(); parent1.Children.Add(new Child() { Name = "One" }); parent1.Children.Add(new Child() { Name = "Two" }); var parent2 = new Parent() { Id = 2 }; parent2.Children = new List<Child>(); parent2.Children.Add(new Child() { Name = "One" }); parent2.Children.Add(new Child() { Name = "Two" }); var parent3 = new Parent() { Id = 3 }; parent3.Children = new List<Child>(); parent3.Children.Add(new Child() { Name = "One" }); parent3.Children.Add(new Child() { Name = "Two" }); var parentShallowCopy = parent1.ShallowCopy(); var parentDeepCopy = parent2.DeepCopy(); var parentDeepClone = parent3.DeepClone(); Console.WriteLine("ORIGINAL(1) : {0}", parent1); Console.WriteLine("SHALLOW COPY: {0}", parentShallowCopy); …

HG Mercurial Quick Setup Tutorial

Setting up Remote (build in) http server for multiple repositories* Create Directory Structure  - repo-root
   |- Project1.hg
   |- Project2.hg
   hgweb.config
   run.bat

* Edit hgweb.config as follows  [collections]
  repos/ = .
  [web]
  style = gitweb
  push_ssl = false
  allow_push = *

* Edit run.bat as follows and run ithg serve --webdir-conf hgweb.configHttp Server is running on http://localhost:8000/
Setting up Remote Repository* Create Folder /repo-root/Project1.hg
* Run hg init from within that folder
Setting up Local Repository* Go to your local project directory /projects
* Clone remote repository locally hg clone http://localhost:8000/Project1.hg Project1If you have an existing project and want to push it to remote repository* Run hg push http://localhost:8000/Project1.hgOr set default remote repository* Create file /projects/Project1/.hg/hgrc
* Add this to file[paths]
  default =
http://localhost:8000/Project1.hg

Simple XML based C# Database

publicclass DatabaseMgr { public Database Database { get; set; } privatestatic DatabaseMgr instance; private DatabaseMgr() { } publicstatic DatabaseMgr Instance { get { if (instance == null) { instance = new DatabaseMgr(); } return instance; } } //use temp pathprivatestaticstring PATH = Path.Combine(Path.GetTempPath(), "UserDatabase.xml"); publicvoid Reload() { if (File.Exists(PATH)) { XmlSerializer xs = new XmlSerializer(typeof(Database)); using (TextReader reader = new StreamReader(PATH)) { Database = (Database)xs.Deserialize(reader); reader.Close(); } } else { Database = new Database(); } } publicvoid Save() { XmlSerializer xmlSerializer = new XmlSerializer(typeof(Database)); using (TextWriter writer = new StreamWriter(PATH)) { xmlSerializer.Serialize(writer, Database); writer.Close(); } } } [Serializable] publicclass Database {

C# Log4Net configuration for multiple processes with date rolling

This example uses MinimalLock locking Model to allow multiple processes to write to the same file<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender"> <file value="Log\\MyApp.log"/> <appendToFile value="true"/> <rollingStyle value="Date"/> <datePattern value=".yyyyMMdd"/> <maxSizeRollBackups value="7"/> <staticLogFileName value="true"/> <threshold value="INFO"/> <lockingModel type="log4net.Appender.FileAppender+MinimalLock" /> <layout type="log4net.Layout.PatternLayout"> <conversionPattern value="%d [%t] %-5p %c.%M():%L - %m%n"/> </layout> </appender>Click here for more detailed post about Log4Net configuration in console apps

C# Send email with embedded image

string mailserver = "mymailserver"; string subject = "Email with picture"; string body = "<html><body><h1>Picture</h1><br><img src=\"cid:Pic1\"></body></html>"; string to = "to@email.com"; string from = "from@email.com"; string imagePath = @"c:\temp\myimage.png"; AlternateView view = AlternateView.CreateAlternateViewFromString(body, null, MediaTypeNames.Text.Html); LinkedResource pic = new LinkedResource(imagePath, MediaTypeNames.Image.Gif); pic.ContentId = "Pic"; view.LinkedResources.Add(pic); MailMessage m = new MailMessage(); m.AlternateViews.Add(view); m.From = new MailAddress(from); m.To.Add(new MailAddress(to)); m.Subject = subject; SmtpClient client = new SmtpClient(mailserver); client.Send(m);

SQL Delete in batches based on date

This is a sample how to delete data in batches to avoid large transaction log. This is a simpler version of this example when I used 2 tables.DECLARE @MyTable TABLE( Id int, DateUpdated DATETIME) INSERTINTO @MyTable SELECT 1,DATEADD(d,-1,GETDATE()) UNIONSELECT 2,DATEADD(d,-2,GETDATE()) UNIONSELECT 3,DATEADD(d,1,GETDATE()) UNIONSELECT 4,DATEADD(d,2,GETDATE()) UNIONSELECT 5,DATEADD(d,-3,GETDATE()) UNIONSELECT 6,DATEADD(d,-2,GETDATE()) UNIONSELECT 7,DATEADD(d,-4,GETDATE()) DECLARE @dateDATETIMESET @date = GETDATE() PRINTCONVERT(CHAR(19),GETDATE(),120) +' Deleting data in batches' WHILE (Exists(SELECT 1 FROM @MyTable

Retry class implemented in functional C#

publicclass Retry { publicstatic ILog _log = LogManager.GetLogger(MethodInfo.GetCurrentMethod().DeclaringType); int _retryAttempts = 3; int _retryWaitSeconds = 1; public Retry() { } public Retry(int retryAttempts, int retryWaitSeconds) { _retryAttempts = retryAttempts; _retryWaitSeconds = retryWaitSeconds; } publicvoid Execute(Action action) { int attempt = 0; while (attempt++ < _retryAttempts) { try { if (_log.IsDebugEnabled) { _log.DebugFormat("Retry attempt {0}/{1}", attempt, _retryAttempts); } action(); return; } catch (Exception e) { if (attempt >= _retryAttempts) { _log.ErrorFormat("Retry failed with {0} attempts", _retryAttempts); throw e; } int wait = _retryWaitSeconds * attempt; _log.Warn(string.Format("Retry attempt {0}/{1} failed, waiting {2}s", attempt, _retryAttempts, wait), e); Thread.Sleep(1000 * wait); } } } publ…