Mongodb

1,链接db:

public class MDb
{
public static string MongoDb_DatabaseName = System.Configuration.ConfigurationManager.AppSettings["MongoDb_DatabaseName"];
public static string MongoDb_Server = System.Configuration.ConfigurationManager.AppSettings["MongoDb_Server"];
public static string MongoDb_User = System.Configuration.ConfigurationManager.AppSettings["MongoDB_User"];
public static string MongoDb_Pass = System.Configuration.ConfigurationManager.AppSettings["MongoDB_Pass"];
//MongoDb 连接字符串
//public static string strMongoDbConfiguration = @"mongodb://" + MongoDb_User + ":" + MongoDb_Pass + "@" + MongoDb_Server + "/" + MongoDb_DatabaseName;

public static string strMongoDbConfiguration = ConfigurationManager.ConnectionStrings["PCMDB"].ConnectionString;
public static MongoDatabase Default
{
get
{
var client = new MongoClient(strMongoDbConfiguration);
return client.GetServer().GetDatabase(MongoDb_DatabaseName);
}
}

public static MongoCollection<T> Collection<T>()
{
return MDb.Default.GetCollection<T>(typeof(T).Name);
}
public static class Collections
{
public static MongoCollection<Products> Products
{
get
{
return MDb.Default.GetCollection<Products>("Products");
}
}
}
public static MongoCollection<Products> Products
{
get
{
return MDb.Collection<Products>();
}
}

 

}

2,创建Entity:

[Serializable]
public class Entity
{
private BsonObjectId _oid = null;

public BsonObjectId Id
{
get { return _oid; }
set
{
_oid = value;
}
}

public override string ToString()
{
return Id.ToString();
}
}

3,建立model:

[Serializable]
public class Products : ICloneable
{
public string Id
{
get;
set;
}

[BsonExtraElements]
public BsonDocument CatchExtraAll { get; set; }

/// <summary>
/// 返回SKU
/// </summary>
public int SKU
{
get;
set;
}
public object Clone()
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, this);
ms.Seek(0, 0);
object value = bf.Deserialize(ms);
ms.Close();
return value;
}
}

最后 附上,log4net的配置:

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "log4net.config", Watch = true)]

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

 

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<log4net>

<root>
<level value="INFO"/>
<appender-ref ref="rootConsole"/>
<appender-ref ref="FileAppender" />
</root>
<appender name="FileAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="..\\Log\\Log.txt" />
<param name="AppendToFile" value="true" />
<param name="MaximumFileSize" value="1024KB" />
<param name="MaxSizeRollBackups" value="10" />
<param name="DatePattern" value="yyyy-MM-dd HH-mm" />
<param name="RollingStyle" value="Size" />
<encoding value="utf-8" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="[%d] [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>

<appender name="ErrorAppender" type="log4net.Appender.RollingFileAppender">
<param name="File" value="..\\Log\\Error.txt" />
<param name="AppendToFile" value="true" />
<param name="MaximumFileSize" value="1024KB" />
<param name="MaxSizeRollBackups" value="10" />
<param name="DatePattern" value="yyyy-MM-dd HH-mm" />
<param name="RollingStyle" value="Size" />
<encoding value="utf-8" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="[%d] [%t] %-5p %c [%x] - %m%n" />
</layout>
</appender>

<appender name="SmtpAppender" type="log4net.Appender.SmtpAppender">
<authentication value="Basic" />
<!--配置接收人的邮箱地址-->
<to value="*@.com" />
<!--配置发送人的邮箱地址-->
<from value="*@*.com" />
<!--主题-->
<subject value="更新***" />
<!--SMTP服务器-->
<smtpHost value="mail.***.com" />
<!--用户名-->
<username value="mail@*.com"/>
<!--密码-->
<password value="***"/>
<bufferSize value="***" />
<lossy value="true" />
<evaluator type="log4net.Core.LevelEvaluator">
<!--All log events with lower level than the threshold level are ignored by the appender-->
<threshold value="INFO"/>
</evaluator>
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%newline%date [%thread] %-5level %logger [%property{NDC}] - %message%newline%newline%newline" />
</layout>
</appender>

<appender name="rootConsole" type="log4net.Appender.ColoredConsoleAppender, log4net">
<threshold value="ALL"/>
<mapping>
<level value="DEBUG"/>
<foreColor value="Green, HighIntensity"/>
</mapping>
<mapping>
<level value="WARN"/>
<foreColor value="White, HighIntensity"/>
</mapping>
<mapping>
<level value="INFO"/>
<foreColor value="Yellow, HighIntensity"/>
</mapping>
<mapping>
<level value="ERROR"/>
<foreColor value="Red, HighIntensity"/>
</mapping>
<layout type="log4net.Layout.PatternLayout,log4net">
<param name="ConversionPattern" value="%message%newline %exception"/>
</layout>
</appender>
</log4net>
</configuration>

 

 

初始化数据库的方法:

public static void InitMongodb()
{
var conventionPack = new ConventionPack();
conventionPack.Add(new NamedIdMemberConvention("id", "_id"));
conventionPack.Add(new IgnoreExtraElementsConvention(true));
conventionPack.Add(new StringObjectIdIdGeneratorConvention());
//枚举序列化为字符串
conventionPack.Add(new EnumRepresentationConvention(BsonType.String));

ConventionRegistry.Register("DefaultConvention", conventionPack, t => true);

}

posted @ 2015-02-11 18:11  小良哥  阅读(277)  评论(0编辑  收藏  举报