Can't configure log4net Logger programmatically - log4net

I need to configure a log4net Logger programmatically. Specifically, I need to add a Logger with one appender. However, when I retrieve it, I find that it doesn't have the correct level or appender, and was hoping I was missing some simple configuration ceremony:
var patternLayout = new PatternLayout { ConversionPattern = "%message%newline" };
patternLayout.ActivateOptions();
var hierarchy = (Hierarchy)LogManager.GetRepository();
var testAppender = new RollingFileAppender
{
Name = "test-appender",
AppendToFile = true,
File = "c:\\temp\\test.log",
Layout = patternLayout,
DatePattern = "yyyyMMdd-HHmm",
MaxSizeRollBackups = 50,
MaximumFileSize = "20MB",
RollingStyle = RollingFileAppender.RollingMode.Size,
StaticLogFileName = false,
Encoding = Encoding.UTF8,
PreserveLogFileNameExtension = true,
Threshold = Level.Debug
};
testAppender.ActivateOptions();
var testLogger = hierarchy.LoggerFactory.CreateLogger(hierarchy, "test");
testLogger.Hierarchy = hierarchy;
testLogger.AddAppender(testAppender);
testLogger.Repository.Configured = true;
testLogger.Level = Level.Debug;
testLogger.Additivity = false;
hierarchy.Configured = true;
var testLogger = LogManager.GetLogger("test");
The logger 'testLogger' has a null Level and no appenders. What am I missing?

Instead of calling
var testLogger = hierarchy.LoggerFactory.CreateLogger(hierarchy, "test");
I called
var testLogger = hierarchy.GetLogger("test", hierarchy.LoggerFactory);
And that worked. GetLogger will create the logger if it does not already exist.

Related

Add dimension between link and element

i am tried to find the link wall face,but when i use the reference to create a new dimension , i will get result about 'invaild number of references'. i cant trans link face to active face.
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uidoc = commandData.Application.ActiveUIDocument;
Document doc = uidoc.Document;
var rf1 = uidoc.Selection.PickObject(ObjectType.PointOnElement, "select");
var element1 = doc.GetElement(rf1);
var location = element1.Location as LocationPoint;
var point = location.Point;
var rf2 = uidoc.Selection.PickObject(ObjectType.LinkedElement, "select");
var linkElement = doc.GetElement(rf2) as RevitLinkInstance;
var linkDoc = linkElement.GetLinkDocument();
var linkWall = linkDoc.GetElement(rf2.LinkedElementId) as Wall;
var wallLocation = linkWall.Location as LocationCurve;
var curve = wallLocation.Curve;
var cRf = curve.Reference;
var solid = BIMTools.Geometry.GetSolid(linkWall);
Face face = null;
foreach (var solidFace in solid.Faces)
{
XYZ normal = ((Face)solidFace).ComputeNormal(new UV(0, 0));
if (normal.Y < 0)
{
face = solidFace as Face;
break;
}
}
var viewLevel = uidoc.ActiveView.GenLevel.Elevation;
var tPoint = new XYZ(point.X,(face as PlanarFace).Origin.Y, viewLevel);
point = new XYZ(point.X, point.Y, viewLevel);
var line = Line.CreateBound(point, tPoint);
var references = new ReferenceArray();
references.Append(rf1);
references.Append(face.Reference);
using (Transaction trans = new Transaction(doc,"create"))
{
trans.Start();
var dimension = doc.Create.NewDimension(uidoc.ActiveView, line, references);
trans.Commit();
}
return Result.Succeeded;
}
The Building Coder provides a whole list of articles on creating dimensioning.

Log4Net deadlock in AppenderSkeleton.DoAppend

I use log4net in a multi threaded service. Andat some point unexpectedly the service stops to respond. I checked the dump of the process and the stack is below.
This is the appender configuration :
var roller = new RollingFileAppender();
roller.LockingModel = new MinimalLock();
roller.File = #"logs\Log_Tabsters_" + appName + "_";
roller.DatePattern = "yyyy-MM-dd'.log'";
roller.StaticLogFileName = false;
roller.AppendToFile = true;
roller.RollingStyle = RollingMode.Composite;
roller.Layout = patternLayout;
roller.MaxSizeRollBackups = 10;
roller.MaximumFileSize = "1GB";
roller.ActivateOptions();
var buffer = new BufferingForwardingAppender();
buffer.BufferSize = 100;
buffer.Lossy = false;
buffer.AddAppender(roller);
buffer.ActivateOptions();
hierarchy.Root.AddAppender(buffer);
var console = new ColoredConsoleAppender();
var mappingError = new LevelColors();
mappingError.Level = Level.Error;
mappingError.ForeColor = Colors.White;
mappingError.BackColor = Colors.Red;
console.AddMapping(mappingError);
var mappingWarn = new LevelColors();
mappingWarn.Level = Level.Warn;
mappingWarn.ForeColor = Colors.White;
mappingWarn.BackColor = Colors.Yellow;
console.AddMapping(mappingWarn);
console.Layout = patternLayout;
console.ActivateOptions();
hierarchy.Root.AddAppender(console);
I see that the threads are stuck in DoAppend
the stack trace is
log4net.dll!log4net.Appender.AppenderSkeleton.DoAppend(log4net.Core.LoggingEvent loggingEvent)
log4net.dll!log4net.Util.AppenderAttachedImpl.AppendLoopOnAppenders(log4net.Core.LoggingEvent loggingEvent)
log4net.dll!log4net.Repository.Hierarchy.Logger.CallAppenders(log4net.Core.LoggingEvent loggingEvent)
log4net.dll!log4net.Repository.Hierarchy.Logger.ForcedLog(System.Type callerStackBoundaryDeclaringType, log4net.Core.Level level, object message, System.Exception exception)
log4net.dll!log4net.Repository.Hierarchy.Logger.Log(System.Type callerStackBoundaryDeclaringType, log4net.Core.Level level, object message, System.Exception exception)
log4net.dll!log4net.Core.LogImpl.Warn(object message)
TabstersCore.dll!Tabsters.Logger.AddLog(string module, string log, Tabsters.LogType logType) Line 129

Log4j 1.x to Log4j 2.1.1 migration issue

I'm trying migrate code to version 2. But, I'm running into some issues with finding an alternative to getCurrentLoggers in log4j 2.1.1
import org.apache.logging.log4j.LogManager.getCurrentLoggers(); does not exist.
private ArrayList<LoggerName> getLoggerList() {
// Generate a list of all the loggers and levels
ArrayList<String> al = new ArrayList<String>();
HashMap<Object, Object> hm = new HashMap<Object, Object>();
ArrayList<LoggerName> list = new ArrayList<LoggerName>();
// Get RootLogger
Logger rootLogger = LogManager.getRootLogger();
String rootLoggerName = rootLogger.getName();
al.add(rootLoggerName);
hm.put(rootLoggerName, rootLogger);
// All Other Loggers ISSUE HERE
Enumeration e = LogManager.getCurrentLoggers();
while (e.hasMoreElements()) {
Logger t1Logger = (Logger) e.nextElement();
String loggerName = t1Logger.getName();
al.add(loggerName);
hm.put(loggerName, t1Logger);
}
String[] alLoggerStr = ((String[]) al.toArray(new String[0]));
Arrays.sort(alLoggerStr);
for (int i=0; i < alLoggerStr.length; i++) {
Logger logger = (Logger) hm.get(alLoggerStr[i]);
String name = logger.getName();
String level = logger.getLevel().toString();
String parent = GenFuncs.EMPTY_STRING;
if (logger.getParent() != null) {
parent = (logger.getParent().getName();
}
LoggerName logData = new LoggerNameImpl(name, parent, level);
list.add(logData);
}
return list;
}
Found answer from Stack Overflow
File configFile = new File("c:\\my_path\\log4j2.xml");
LoggerContext loggerContext = Configurator.initialize("my_config", null, configFile.toURI());
Configuration configuration = loggerContext.getConfiguration();
Collection<LoggerConfig> loggerConfigs = configuration.getLoggers().values();

Remove IP Address from IP Security <ipSecurity> IIS8.0

I am using iis 8.0 and trying to remove any allowed/restricted ip address from the list, screen shot is attached. i have used the remove variable by following this link.
var websiteName = "abc.com";
using (var serverManager = new ServerManager())
{
var config = serverManager.GetApplicationHostConfiguration();
var ipSecuritySection = config.GetSection("system.webServer/security/ipSecurity", websiteName);
var ipSecurityCollection = ipSecuritySection.GetCollection();
var addElement = ipSecurityCollection.CreateElement("remove");
addElement["ipAddress"] = ipAddress;
ipSecurityCollection.Remove(addElement);
serverManager.CommitChanges();
}
Guide me am i doing wrong, if yes? then what is it.
I have found the way to remove entry from the collection.
var websiteName = "abc.com";
using (var serverManager = new ServerManager())
{
var config = serverManager.GetApplicationHostConfiguration();
var ipSecuritySection = config.GetSection("system.webServer/security/ipSecurity", websiteName);
var ipSecurityCollection = ipSecuritySection.GetCollection();
var addElement = ipSecurityCollection.CreateElement("remove");
addElement["ipAddress"] = ipAddress;
ipSecurityCollection.add(addElement);
serverManager.CommitChanges();
}
Every thing was alright only one thing was wrong in the 2nd last line.
ipSecurityCollection.remove(addElement);
Change it to
ipSecurityCollection.add(addElement);
After that it will work perfectly.

NetSuite SuiteTalk API - Get Inventory Details

I'm using the SuiteTalk (API) service for NetSuite to retrieve a list of Assemblies. I need to load the InventoryDetails fields on the results to view the serial/lot numbers assigned to the items. This is the current code that I'm using, but the results still show those fields to come back as NULL, although I can see the other fields for the AssemblyBuild object. How do I get the inventory details (serials/lot#'s) to return on a transaction search?
public static List<AssemblyBuildResult> Get()
{
var listAssemblyBuilds = new List<AssemblyBuildResult>();
var service = Service.Context();
var ts = new TransactionSearch();
var tsb = new TransactionSearchBasic();
var sfType = new SearchEnumMultiSelectField
{
#operator = SearchEnumMultiSelectFieldOperator.anyOf,
operatorSpecified = true,
searchValue = new string[] { "_assemblyBuild" }
};
tsb.type = sfType;
ts.basic = tsb;
ts.inventoryDetailJoin = new InventoryDetailSearchBasic();
// perform the search
var response = service.search(ts);
response.pageSizeSpecified = true;
// Process response
if (response.status.isSuccess)
{
// Process the records returned in the response
// Get more records with pagination
if (response.totalRecords > 0)
{
for (var x = 1; x <= response.totalPages; x++)
{
var records = response.recordList;
foreach (var t in records)
{
var ab = (AssemblyBuild) t;
listAssemblyBuilds.Add(GetAssemblyBuildsResult(ab));
}
if (response.pageIndex < response.totalPages)
{
response = service.searchMoreWithId(response.searchId, x + 1);
}
}
}
}
// Parse and return NetSuite WorkOrder into assembly WorkOrderResult list
return listAssemblyBuilds;
}
After much pain and suffering, I was able to solve this problem with the following code:
/// <summary>
/// Returns List of AssemblyBuilds from NetSuite
/// </summary>
/// <returns></returns>
public static List<AssemblyBuildResult> Get(string id = "", bool getDetails = false)
{
// Object to populate and return results
var listAssemblyBuilds = new List<AssemblyBuildResult>();
// Initiate Service and SavedSearch (TransactionSearchAdvanced)
var service = Service.Context();
var tsa = new TransactionSearchAdvanced
{
savedSearchScriptId = "customsearch_web_assemblysearchmainlist"
};
// Filter by ID if specified
if (id != "")
{
tsa.criteria = new TransactionSearch()
{
basic = new TransactionSearchBasic()
{
internalId = new SearchMultiSelectField
{
#operator = SearchMultiSelectFieldOperator.anyOf,
operatorSpecified = true,
searchValue = new[] {
new RecordRef() {
type = RecordType.assemblyBuild,
typeSpecified = true,
internalId = id
}
}
}
}
};
}
// Construct custom columns to return
var tsr = new TransactionSearchRow();
var tsrb = new TransactionSearchRowBasic();
var orderIdCols = new SearchColumnSelectField[1];
var orderIdCol = new SearchColumnSelectField();
orderIdCols[0] = orderIdCol;
tsrb.internalId = orderIdCols;
var tranDateCols = new SearchColumnDateField[1];
var tranDateCol = new SearchColumnDateField();
tranDateCols[0] = tranDateCol;
tsrb.tranDate = tranDateCols;
var serialNumberCols = new SearchColumnStringField[1];
var serialNumberCol = new SearchColumnStringField();
serialNumberCols[0] = serialNumberCol;
tsrb.serialNumbers = serialNumberCols;
// Perform the Search
tsr.basic = tsrb;
tsa.columns = tsr;
var response = service.search(tsa);
// Process response
if (response.status.isSuccess)
{
var searchRows = response.searchRowList;
if (searchRows != null && searchRows.Length >= 1)
{
foreach (SearchRow t in searchRows)
{
var transactionRow = (TransactionSearchRow)t;
listAssemblyBuilds.Add(GetAssemblyBuildsResult(transactionRow, getDetails));
}
}
}
// Parse and return NetSuite WorkOrder into assembly WorkOrderResult list
return listAssemblyBuilds;
}
private static string GetAssemblyBuildLotNumbers(string id)
{
var service = Service.Context();
var serialNumbers = "";
var tsa = new TransactionSearchAdvanced
{
savedSearchScriptId = "customsearch_web_assemblysearchlineitems"
};
service.searchPreferences = new SearchPreferences { bodyFieldsOnly = false };
tsa.criteria = new TransactionSearch()
{
basic = new TransactionSearchBasic()
{
internalId = new SearchMultiSelectField
{
#operator = SearchMultiSelectFieldOperator.anyOf,
operatorSpecified = true,
searchValue = new[] {
new RecordRef() {
type = RecordType.assemblyBuild,
typeSpecified = true,
internalId = id
}
}
}
}
};
// Construct custom columns to return
var tsr = new TransactionSearchRow();
var tsrb = new TransactionSearchRowBasic();
var orderIdCols = new SearchColumnSelectField[1];
var orderIdCol = new SearchColumnSelectField();
orderIdCols[0] = orderIdCol;
tsrb.internalId = orderIdCols;
var serialNumberCols = new SearchColumnStringField[1];
var serialNumberCol = new SearchColumnStringField();
serialNumberCols[0] = serialNumberCol;
tsrb.serialNumbers = serialNumberCols;
tsr.basic = tsrb;
tsa.columns = tsr;
var response = service.search(tsa);
if (response.status.isSuccess)
{
var searchRows = response.searchRowList;
if (searchRows != null && searchRows.Length >= 1)
{
foreach (SearchRow t in searchRows)
{
var transactionRow = (TransactionSearchRow)t;
if (transactionRow.basic.serialNumbers != null)
{
return transactionRow.basic.serialNumbers[0].searchValue;
}
}
}
}
return serialNumbers;
}
private static AssemblyBuildResult GetAssemblyBuildsResult(TransactionSearchRow tsr, bool getDetails)
{
if (tsr != null)
{
var assemblyInfo = new AssemblyBuildResult
{
NetSuiteId = tsr.basic.internalId[0].searchValue.internalId,
ManufacturedDate = tsr.basic.tranDate[0].searchValue,
SerialNumbers = tsr.basic.serialNumbers[0].searchValue
};
// If selected, this will do additional NetSuite queries to get detailed data (slower)
if (getDetails)
{
// Look up Lot Number
assemblyInfo.LotNumber = GetAssemblyBuildLotNumbers(tsr.basic.internalId[0].searchValue.internalId);
}
return assemblyInfo;
}
return null;
}
What I learned about pulling data from NetSuite:
Using SavedSearches is the best method to pull data that doesn't automatically come through in the API objects
It is barely supported
Don't specify an ID on the SavedSearch, specify a criteria in the TransactionSearch to get one record
You will need to specify which columns to actually pull down. NetSuite doesn't just send you the data from a SavedSearch automatically
You cannot view data in a SavedSearch that contains a Grouping
In the Saved Search, use the Criteria Main Line = true/false to read data from the main record (top of UI screen), and line items (bottom of screen)

Resources